You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Event Hub连接报错[Errno 9] Bad file descriptor求助

Azure Event Hub连接错误:Errno 9 Bad file descriptor 排查方案

我有一个向Azure Event Hub发送数据的独立脚本,数月前运行正常,现在执行时抛出如下错误:

Traceback (most recent call last):
  File "test.py", line 82, in <module>
    getAirThings()
  File "test.py", line 76, in getAirThings
    event_data_batch = producer.create_batch()
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/azure/eventhub/_producer_client.py", line 740, in create_batch
    self._get_max_message_size()
  File ".venv/lib/python3.11/site-packages/azure/eventhub/_producer_client.py", line 336, in _get_max_message_size
    )._open_with_retry()
     ^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/azure/eventhub/_producer.py", line 155, in _open_with_retry
    return self._do_retryable_operation(self._open, operation_need_param=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/azure/eventhub/_client_base.py", line 608, in _do_retryable_operation
    raise last_exception from None

azure.eventhub.exceptions.ConnectError: Failed to initiate the connection due to exception: [Errno 9] Bad file descriptor
 Error condition: ErrorCondition.SocketError
 Error Description: Failed to initiate the connection due to exception: [Errno 9] Bad file descriptor

排查步骤

  • 检查Producer客户端生命周期:确保每次发送完成后调用producer.close()关闭客户端,避免socket资源泄漏。如果脚本中重复创建Producer但未关闭,会耗尽系统文件句柄,触发该错误。
  • 升级Azure Event Hub SDK:当前使用的SDK版本可能存在已知的资源泄漏问题,执行以下命令更新到最新版:
    pip install --upgrade azure-eventhub
    
  • 验证系统文件句柄限制:用ulimit -n(Linux/macOS)查看系统允许的最大文件描述符数,确认脚本运行时未超出限制。Windows可通过任务管理器查看进程的句柄计数。
  • 检查网络与权限:确认运行环境网络可正常访问Azure Event Hub端点,防火墙/代理未拦截连接;同时验证连接字符串中的密钥未过期、权限配置正确。
  • 检查脚本逻辑:确认脚本中没有重复初始化Producer的逻辑,或存在未正确释放资源的代码分支(如异常未捕获导致资源未关闭)。

内容的提问来源于stack exchange,提问作者datatalian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 00:22:11