Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复断网重连以后录制无法自动恢复的问题 #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

imkero
Copy link

@imkero imkero commented Apr 23, 2024

Fix #259

问题现象

断网触发 ConnectionErrorHandler 后:

  • 若网络恢复,会报错 aiohttp.client_exceptions.ClientResponseError: 412, message='Precondition Failed', url=URL('https://live.bilibili.com/')
  • 若网络一直未恢复,断网等待时间 设置项不生效

问题原因

  1. Livecheck_connectivity 方法未捕获所有异常,若请求 https://live.bilibili.com/ 失败,会导致异常抛出到 ConnectionErrorHandler_wait_for_connection_error 导致重试与超时逻辑被打断

    async def check_connectivity(self) -> bool:
    try:
    await self._session.head('https://live.bilibili.com/', timeout=3)
    except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
    return False
    else:
    return True

    def _wait_for_connection_error(self) -> bool:
    timeout = self.disconnection_timeout
    logger.info(f'Waiting {timeout} seconds for connection recovery... ')
    timebase = time.monotonic()
    while not self._call_coroutine(self._live.check_connectivity()):
    if timeout is not None and time.monotonic() - timebase > timeout:
    logger.error(f'Connection not recovered in {timeout} seconds')
    return False
    time.sleep(self.check_interval)
    else:
    logger.info('Connection recovered')
    return True

  2. Livecheck_connectivity 方法请求的 B 站直播首页 https://live.bilibili.com/ 有风控策略,当请求 UA 为 python requests 的 UA(例如 python-requests/2.31.0)时,会拦截并返回 412 状态码,导致 check_connectivity 始终不能成功

    curl -v -H "User-Agent: python-requests/2.31.0" https://live.bilibili.com/
    
    > GET / HTTP/1.1
    > Host: live.bilibili.com
    > Accept: */*
    > User-Agent: python-requests/2.31.0
    >
    
    < HTTP/1.1 412 Precondition Failed
    < Content-Type: text/html
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    < Server: openresty
    < Cache-Control: no-cache
    < X-BILI-SEC-TOKEN: 1,denied by waf mode
    <
    

修复方法

  1. check_connectivity 请求 https://live.bilibili.com/ 时带上用户指定的 UA
  2. check_connectivity 捕获 requests 请求时抛出的所有异常,若有异常则返回 False 并打印异常信息到 log 当中

@imkero imkero changed the title fix: live check_connectivity behaviour 修复断网重连以后录制无法自动恢复的问题 Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

断网重连以后录制无法自动恢复
1 participant