Gunicorn: OSError: [Errno 9] 错误的文件描述符

创建于 2018-09-10  ·  19评论  ·  资料来源: benoitc/gunicorn

我在 raspbian 图像中遇到了这个问题

[2018-09-10 20:40:11 +0800] [21421] [关键] 工人超时 (pid:21699)
[2018-09-10 20:40:11 +0800] [21699] [ERROR] 套接字错误处理请求。
回溯(最近一次调用最后一次):
文件“/usr/lib/python3/dist-packages/gunicorn/workers/async.py”,第62行,句柄
六.重新加注(*sys.exc_info())
文件“/usr/lib/python3/dist-packages/gunicorn/six.py”,第 625 行,重新加注
提升价值
文件“/usr/lib/python3/dist-packages/gunicorn/workers/async.py”,第35行,句柄
listener_name = listener.getsockname()
OSError: [Errno 9] 错误的文件描述符

Feedback Requested Investigation

最有用的评论

  1. 我可以通过仅使用gunicornuvicorn的设置来重现此日志错误。 此错误消息开始出现在uvicorn==0.11.4而不是之前的版本0.11.3 (在 OSx 和 Linux 容器中)。 这与上面关于 uvicorn 的报告一致,报告的版本总是大于0.11.4 。 最后的证据
  2. 造成此错误的提交是 this one 。 问题在于刚才提到的这几行提交。 提交仅更改两个代码块的顺序。 如果我恢复顺序更改,日志错误就会消失,同时仍然通过uvicorn的测试套件
  3. 如果存在以下任一情况,则会发生相同的日志错误:如上所述,在堆栈gunicorn+uvicorn顶部使用starlettefastapi ; - 运行最新的 uvicorn 版本12.X而不是0.11.4 ; - 与不止一个uvicorn工人一起运行gunicorn

证据。 在 osx 上的新文件夹中运行附加脚本test.sh (在 osx 上测试)。 要在 Linux 容器中进行测试,请保存脚本和 Dockerfile,然后读取 Dockerfile 的标头。 我还附上了脚本的日志。

@benoitc ,您如何看待uvicorn中似乎引入了该错误的提交? 问题似乎出在gunicornuvicorn之间的接口上。 您能评论一下uvicorn中上述提交中更改的 2 个代码块的顺序吗? 这可能有助于找出其他情况下也会发生这种情况的原因。 到目前为止,这也与aiohttpgeventFlask-SocketIO sanic一起报道。 为方便起见,我还附上了脚本的日志。

log_test.log

文件_test.sh_

#!/bin/bash
python3 -m venv venv
source venv/bin/activate
pip install gunicorn==20.0.04 uvicorn==0.11.4
# create simple uvicorn app
printf "async def app(scope, receive, send):\n    await send()\n" > example.py
# spin up gunicorn with 1 uvicorn worker, and then send TERM signal to gunicorn
gunicorn example:app -w 1 -k uvicorn.workers.UvicornWorker &
sleep 5 && pkill -f gunicorn
sleep 1
# you will see 1 log entry like this one:
# [XX] [YY] [INFO] Error while closing socket [Errno 9] Bad file descriptor

printf "\n\n[INFO] if you instead bump down uvicorn's version from 11.4 to 11.3 [Errno 9] goes away:\n\n"
pip install uvicorn==0.11.3
gunicorn example:app -w 1 -k uvicorn.workers.UvicornWorker &
sleep 5 && pkill -f gunicorn

文件 _Dockerfile_

# run with:
# docker run -it $(docker build -q .)
FROM python:3.8
COPY test.sh .
RUN chmod +x /test.sh
CMD /test.sh

所有19条评论

@leond08谢谢你的票!

为了了解它是如何发生的,您能否提供更多信息?

  • 您正在测试哪个版本的 Gunicorn
  • 你在使用哪个工人
  • 这什么时候发生?

我正在使用 gunicorn3 最新版本
我为此使用 eventlet 和 gevent
我正在运行我的烧瓶应用程序 - Flask-SocketIO

我在用户单击按钮后开始我的后台任务
我的后台任务功能是监听一个事件,
单击“完成”按钮后,后台任务必须停止
然后向所有用户发送一个发送消息

与 aiohttp + gunicorn 有同样的问题,每次 ctrl + c 时观察到相同的消息。

[信息] 关闭套接字时出错 [Errno 9] 文件描述符错误

我不复制它。 我怀疑您的应用程序正在关闭一些导致上述问题的 fd。

我们遇到了同样的问题,唯一的问题是它只发生在 docker swarm 中运行的 8 个容器中的 1 个上。

我们在 9 个容器中有 1 个遇到了同样的问题,它看起来与 docker、python3 和 gevent 相关。

gunicorn 20.0.4 + aiohttp 3.6.2

Gunicorn 作为开发服务器运行:

gunicorn --reload app:make_app --bind localhost:5000 --worker-class aiohttp.GunicornWebWorker --workers 2 --access-logfile -

几乎每个 Ctrl+C 都以

^C[2020-05-23 21:49:50 +0200] [38524] [INFO] Handling signal: int
Exception ignored when trying to write to the signal wakeup fd:
Exception ignored when trying to write to the signal wakeup fd:
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/unix_events.py", line 42, in _sighandler_noop
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/unix_events.py", line 42, in _sighandler_noop
    def _sighandler_noop(signum, frame):
    def _sighandler_noop(signum, frame):
OSError: [Errno 9] Bad file descriptor
OSError: [Errno 9] Bad file descriptor
[2020-05-23 21:49:50 +0200] [38526] [INFO] Worker exiting (pid: 38526)
[2020-05-23 21:49:50 +0200] [38528] [INFO] Worker exiting (pid: 38528)
[2020-05-23 21:49:50 +0200] [38524] [INFO] Shutting down: Master

应用程序是否处理任何请求并不重要。

使用 Sanic 20.3.0:

^C[2020-05-26 13:24:55 +0200] [27706] [INFO] Handling signal: int
[2020-05-26 13:24:55 +0200] [27769] [INFO] Stopping server: 27769, connections: 0
[2020-05-26 13:24:55 +0200] [27769] [INFO] Error while closing socket [Errno 9] Bad file descriptor
[2020-05-26 13:24:55 +0200] [27769] [INFO] Worker exiting (pid: 27769)
[2020-05-26 13:24:55 +0200] [27771] [INFO] Stopping server: 27771, connections: 0
[2020-05-26 13:24:55 +0200] [27771] [INFO] Error while closing socket [Errno 9] Bad file descriptor
[2020-05-26 13:24:55 +0200] [27771] [INFO] Worker exiting (pid: 27771)
[2020-05-26 13:24:55 +0200] [27706] [INFO] Shutting down: Master

与每个 Ctrl+C 上的 Gunicorn 20.0.4 + Uvicorn 0.11.5 工人班相同

INFO:     [12621] [gunicorn.error] Handling signal: int
INFO:     [12635] [gunicorn.error] Error while closing socket [Errno 9] Bad file descriptor
INFO:     [12634] [gunicorn.error] Error while closing socket [Errno 9] Bad file descriptor
INFO:     [12635] [gunicorn.error] Worker exiting (pid: 12635)
INFO:     [12634] [gunicorn.error] Worker exiting (pid: 12634)
INFO:     [12621] [gunicorn.error] Shutting down: Master

任何应用示例? 另外我们在谈论哪个版本的 Python?

Ubuntu 20.04,系统在 virtualenv 中提供了 Python 3.8.2

示例应用程序: https : https://github.com/zgoda/newsloop-server/tree/b2a8a7f09fa9834b4f09fa9834b4f09fa9834b48dc 在我之前的评论中确切地调用了 gunicorn 。

aiohttp 和 Sanic 之间的输出差异让我怀疑工作人员有问题。

同样的问题,python 3.8.0
sanic 19.12.2
枪炮 20.0.4

编辑:当我在 Mac 上本地运行时会发生这种情况,但在 Linux docker 中运行时不会发生这种情况,可能会对您有所帮助

你好,
我想这个问题https://github.com/benoitc/gunicorn/issues/2064有同样的原因。
我们有与 issue 几乎相同的错误,但我们使用 gunicorn - 19.9.0

我也遇到过这种情况,FastAPI + 最新的 Gunicorn 和 uvicorn 工人与 Python 3.8.5

一旦我停止使用 uvicorn(即从我的 gunicorn 配置中删除这一行):

worker_class = "uvicorn.workers.UvicornWorker"

错误消失。

如上所述,当使用 Ctrl+C 停止 Gunicorn 或向 PID 发送优雅的终止信号时会发生这种情况。

[2020-09-12 11:56:37 +1000] [100390] [INFO] Starting gunicorn 20.0.4
[2020-09-12 11:56:37 +1000] [100390] [INFO] Listening at: http://0.0.0.0:6000 (100390)
[2020-09-12 11:56:37 +1000] [100390] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-09-12 11:56:37 +1000] [100392] [INFO] Booting worker with pid: 100392
[2020-09-12 11:56:38 +1000] [100392] [INFO] Started server process [100392]
[2020-09-12 11:56:38 +1000] [100392] [INFO] Waiting for application startup.
[2020-09-12 11:56:38 +1000] [100392] [INFO] Application startup complete.
[2020-09-12 11:56:48 +1000] [100390] [INFO] Handling signal: term
[2020-09-12 11:56:48 +1000] [100392] [INFO] Shutting down
[2020-09-12 11:56:48 +1000] [100392] [INFO] Error while closing socket [Errno 9] Bad file descriptor
[2020-09-12 11:56:48 +1000] [100392] [INFO] Waiting for application shutdown.
[2020-09-12 11:56:48 +1000] [100392] [INFO] Application shutdown complete.
[2020-09-12 11:56:48 +1000] [100392] [INFO] Finished server process [100392]
[2020-09-12 11:56:48 +1000] [100392] [INFO] Worker exiting (pid: 100392)
[2020-09-12 11:56:48 +1000] [100390] [INFO] Shutting down: Master

这是该问题的精确复制:

[fots<strong i="6">@workstation</strong> testing]$ python3.8 -V
Python 3.8.5
[fots<strong i="7">@workstation</strong> testing]$ python3.8 -m venv ~/.virtualenv/testing
[fots<strong i="8">@workstation</strong> testing]$ source ~/.virtualenv/testing/bin/activate
(testing) [fots<strong i="9">@workstation</strong> testing]$ pip install fastapi gunicorn uvicorn
Collecting fastapi
  Using cached fastapi-0.61.1-py3-none-any.whl (48 kB)
Collecting gunicorn
  Using cached gunicorn-20.0.4-py2.py3-none-any.whl (77 kB)
Collecting uvicorn
  Using cached uvicorn-0.11.8-py3-none-any.whl (43 kB)
Collecting pydantic<2.0.0,>=1.0.0
  Using cached pydantic-1.6.1-cp38-cp38-manylinux2014_x86_64.whl (11.5 MB)
Collecting starlette==0.13.6
  Using cached starlette-0.13.6-py3-none-any.whl (59 kB)
Requirement already satisfied: setuptools>=3.0 in /home/fots/.virtualenv/testing/lib/python3.8/site-packages (from gunicorn) (47.1.0)
Collecting h11<0.10,>=0.8
  Using cached h11-0.9.0-py2.py3-none-any.whl (53 kB)
Collecting websockets==8.*
  Using cached websockets-8.1-cp38-cp38-manylinux2010_x86_64.whl (78 kB)
Collecting httptools==0.1.*; sys_platform != "win32" and sys_platform != "cygwin" and platform_python_implementation != "PyPy"
  Using cached httptools-0.1.1-cp38-cp38-manylinux1_x86_64.whl (227 kB)
Collecting uvloop>=0.14.0; sys_platform != "win32" and sys_platform != "cygwin" and platform_python_implementation != "PyPy"
  Using cached uvloop-0.14.0-cp38-cp38-manylinux2010_x86_64.whl (4.7 MB)
Collecting click==7.*
  Using cached click-7.1.2-py2.py3-none-any.whl (82 kB)
Installing collected packages: pydantic, starlette, fastapi, gunicorn, h11, websockets, httptools, uvloop, click, uvicorn
Successfully installed click-7.1.2 fastapi-0.61.1 gunicorn-20.0.4 h11-0.9.0 httptools-0.1.1 pydantic-1.6.1 starlette-0.13.6 uvicorn-0.11.8 uvloop-0.14.0 websockets-8.1
WARNING: You are using pip version 20.1.1; however, version 20.2.3 is available.
You should consider upgrading via the '/home/fots/.virtualenv/testing/bin/python3.8 -m pip install --upgrade pip' command.
(testing) [fots<strong i="10">@workstation</strong> testing]$ ls -l
total 4
-rw-rw-r-- 1 fots fots 117 Sep 12 12:13 main.py
(testing) [fots<strong i="11">@workstation</strong> testing]$ cat main.py
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}
(testing) [fots<strong i="12">@workstation</strong> testing]$ gunicorn -k uvicorn.workers.UvicornWorker main:app
[2020-09-12 12:19:05 +1000] [105788] [INFO] Starting gunicorn 20.0.4
[2020-09-12 12:19:05 +1000] [105788] [INFO] Listening at: http://127.0.0.1:8000 (105788)
[2020-09-12 12:19:05 +1000] [105788] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-09-12 12:19:05 +1000] [105790] [INFO] Booting worker with pid: 105790
[2020-09-12 12:19:05 +1000] [105790] [INFO] Started server process [105790]
[2020-09-12 12:19:05 +1000] [105790] [INFO] Waiting for application startup.
[2020-09-12 12:19:05 +1000] [105790] [INFO] Application startup complete.
^C[2020-09-12 12:19:06 +1000] [105788] [INFO] Handling signal: int
[2020-09-12 12:19:06 +1000] [105790] [INFO] Shutting down
[2020-09-12 12:19:06 +1000] [105790] [INFO] Error while closing socket [Errno 9] Bad file descriptor
[2020-09-12 12:19:06 +1000] [105790] [INFO] Waiting for application shutdown.
[2020-09-12 12:19:06 +1000] [105790] [INFO] Application shutdown complete.
[2020-09-12 12:19:06 +1000] [105790] [INFO] Finished server process [105790]
[2020-09-12 12:19:06 +1000] [105790] [INFO] Worker exiting (pid: 105790)
[2020-09-12 12:19:07 +1000] [105788] [INFO] Shutting down: Master

这是pip freeze输出:

click==7.1.2
fastapi==0.61.1
gunicorn==20.0.4
h11==0.9.0
httptools==0.1.1
pydantic==1.6.1
starlette==0.13.6
uvicorn==0.11.8
uvloop==0.14.0
websockets==8.1

我尝试从 GitHub(主分支)安装 uvicorn 和 gunicorn,以确保我获得了最新的修复程序,但问题仍然存在。

希望这可以帮助
福蒂斯

  1. 我可以通过仅使用gunicornuvicorn的设置来重现此日志错误。 此错误消息开始出现在uvicorn==0.11.4而不是之前的版本0.11.3 (在 OSx 和 Linux 容器中)。 这与上面关于 uvicorn 的报告一致,报告的版本总是大于0.11.4 。 最后的证据
  2. 造成此错误的提交是 this one 。 问题在于刚才提到的这几行提交。 提交仅更改两个代码块的顺序。 如果我恢复顺序更改,日志错误就会消失,同时仍然通过uvicorn的测试套件
  3. 如果存在以下任一情况,则会发生相同的日志错误:如上所述,在堆栈gunicorn+uvicorn顶部使用starlettefastapi ; - 运行最新的 uvicorn 版本12.X而不是0.11.4 ; - 与不止一个uvicorn工人一起运行gunicorn

证据。 在 osx 上的新文件夹中运行附加脚本test.sh (在 osx 上测试)。 要在 Linux 容器中进行测试,请保存脚本和 Dockerfile,然后读取 Dockerfile 的标头。 我还附上了脚本的日志。

@benoitc ,您如何看待uvicorn中似乎引入了该错误的提交? 问题似乎出在gunicornuvicorn之间的接口上。 您能评论一下uvicorn中上述提交中更改的 2 个代码块的顺序吗? 这可能有助于找出其他情况下也会发生这种情况的原因。 到目前为止,这也与aiohttpgeventFlask-SocketIO sanic一起报道。 为方便起见,我还附上了脚本的日志。

log_test.log

文件_test.sh_

#!/bin/bash
python3 -m venv venv
source venv/bin/activate
pip install gunicorn==20.0.04 uvicorn==0.11.4
# create simple uvicorn app
printf "async def app(scope, receive, send):\n    await send()\n" > example.py
# spin up gunicorn with 1 uvicorn worker, and then send TERM signal to gunicorn
gunicorn example:app -w 1 -k uvicorn.workers.UvicornWorker &
sleep 5 && pkill -f gunicorn
sleep 1
# you will see 1 log entry like this one:
# [XX] [YY] [INFO] Error while closing socket [Errno 9] Bad file descriptor

printf "\n\n[INFO] if you instead bump down uvicorn's version from 11.4 to 11.3 [Errno 9] goes away:\n\n"
pip install uvicorn==0.11.3
gunicorn example:app -w 1 -k uvicorn.workers.UvicornWorker &
sleep 5 && pkill -f gunicorn

文件 _Dockerfile_

# run with:
# docker run -it $(docker build -q .)
FROM python:3.8
COPY test.sh .
RUN chmod +x /test.sh
CMD /test.sh

我有完全相同的问题。 这是我的情况。

简介:我正在尝试使用 gunicorn 为 Django dwebsocket 建立一个测试应用程序。 当我尝试使用 websocket_client 测试结果时,关闭 websocket 后每次都会发生此错误。

环境 :
Docker 镜像: python:3.7
蟒蛇版本:python3.7.6
gunicorn:版本= 20.0.4,工作= gevent
Django 版本:Django==2.2
dwebsocket 版本:0.5.12

代码:

查看.py

from dwebsocket import accept_websocket

<strong i="16">@accept_websocket</strong>
def my_ws(request):
    if request.is_websocket():
        ws = request.websocket
        while True:
            msg = ws.wait(timeout=15)
            if msg is None:
                print('get None message')
                break
            else:
                msg = b'echo :' + msg
                ws.send(msg)
                print('send ws seccess')
        print('websocket close')

网址.py

from websocketInfo.views import  my_ws
from django.conf.urls import url

urlpatterns = [
    url(r'my_ws/$', my_ws, name='my_ws')
]

websocket_client

from websocket import create_connection
ws = create_connection("ws://127.0.0.1:8080/my_ws/")
print("Sending 'Hello, World'...")
ws.send("Hello, World")
print("Receiving...")
result = ws.recv()
print(result)
ws.close()
print('ws close')

命令运行 gunicorn 服务器

gunicorn MyWebsocket.wsgi -b 0.0.0.0:8000 -w 3 -k gevent

错误输出

send ws seccess
get None message
websocket close
[2021-01-13 02:43:56 +0000] [101] [ERROR] Socket error processing request.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 65, in handle
    util.reraise(*sys.exc_info())
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 625, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 55, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/ggevent.py", line 143, in handle_request
    super().handle_request(listener_name, req, sock, addr)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 128, in handle_request
    util.reraise(*sys.exc_info())
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 625, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 114, in handle_request
    resp.write(item)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/http/wsgi.py", line 326, in write
    self.send_headers()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/http/wsgi.py", line 322, in send_headers
    util.write(self.sock, util.to_bytestring(header_str, "latin-1"))
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 286, in write
    sock.sendall(data)
  File "/usr/local/lib/python3.7/site-packages/gevent/_socket3.py", line 523, in sendall
    return _socketcommon._sendall(self, data_memory, flags)
  File "/usr/local/lib/python3.7/site-packages/gevent/_socketcommon.py", line 367, in _sendall
    chunk_size = max(socket.getsockopt(SOL_SOCKET, SO_SNDBUF), 1024 * 1024)
  File "/usr/local/lib/python3.7/site-packages/gevent/_socket3.py", line 156, in __getattr__
    return getattr(self._sock, name)
  File "/usr/local/lib/python3.7/site-packages/gevent/_socket3.py", line 66, in _dummy
    raise OSError(EBADF, 'Bad file descriptor')
OSError: [Errno 9] Bad file descriptor

@ChrisXiaoShu您发布的堆栈跟踪告诉我们,这个特定的套接字对象已在 Python 级别显式关闭(即 gevent 使用其_dummy生成与操作系统相同的异常时)。 这意味着您的应用程序堆栈的某些部分在返回响应以让 gunicorn 处理它之前关闭套接字; 在发生错误时,gunicorn 甚至还没有发送 HTTP 响应标头。

在我的情况下是同样的问题,不同之处在于这个错误没有做任何事情就发生了。 有时5分钟后,有时2小时后...

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

leonardbinet picture leonardbinet  ·  4评论

Abraxos picture Abraxos  ·  4评论

benoitc picture benoitc  ·  4评论

twosigmajab picture twosigmajab  ·  4评论

haolujun picture haolujun  ·  3评论