resurrected asyncio `_SelectorTransport` unregisters fds it doesn't own
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
Short story:
- Socket is closed in
_SelectorTransport.__del__, so it doesn't own the fd anymore. - Anything else in the process can create a new file descriptor at this point.
- If the transport is gc-resurrected to call
close(), it can then remove the reader/writer fromloop._selectorhere or here for a fd it doesn't control anymore, causing access to that fd to hang.
Long story:
- Introduce a GC cycle, such that an
asyncio.selector_events._SelectorTransportand the object owning it are collected at the same time. - The transport
__del__will be called, closing the socket, freeing its file descriptor, but not performing any other cleanup. - The owning object, being a good citizen, resurrects the transport during gc to have
close()called on it properly. In the case of this issue, an asyncio task was created to close it later. (I definitely don't love seeingasyncio.create_taskin a__del__method, but I expect that's not the only way to trigger this) - At this point, anything in your asyncio runloop can open a file descriptor. In my case, it was
asyncio.create_subprocess_execcallingos.pidfd_create()inPidfdChildWatcher.add_child_handler. If you're lucky (or running a lot of tasks at once), the new fd will be the same as the resurrected socket's fd. - Now
transport.close()can run any time later, which will callloop._selector.remove_child_watcher(fd), despite not really owning the fd anymore. - Now the pidfd has been removed from
loop._selector, soawait proc.wait()will hang forever. Or whatever asyncio stream you were trying to use got removed from the selector and you won't be able to wait on it.
I definitely think the non-stdlib code in play here was unsound, but I don't think that should trigger such a cursed result in the stdlib, so I'd rather defend against this in asyncio.
I think this is the minimum patch to mitigate the issue, to prevent the remove_child_reader and remove_child_writer calls by breaking their pre-conditions. I'm not sure if it's worth trying to interact with the loop or selector from __del__ to clean up the fd. (Selecting on a missing fd is probably handled somewhere else anyway?)
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -871,6 +871,8 @@ def close(self):
def __del__(self, _warn=warnings.warn):
if self._sock is not None:
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
+ self._closing = True
+ self._buffer.clear()
self._sock.close()
if self._server is not None:
self._server._detach(self)
Another consideration is that asyncio is using a cached self._sock_fd here without really knowing if the underlying socket was closed and fd was recycled or not. I think this isn't the first time the cached fd has caused a surprising behavior, see also https://github.com/python/cpython/issues/88968
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
- gh-130142
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先閱讀 Lib/asyncio/selector_events.py 中 _SelectorTransport 的清理路徑,尤其是 close() 和 __del__,以及相關的快取 FD issue #88968。重現報告中描述的 FD 重用情境,並驗證復活的 transport 不再從 selector 中取消註冊已回收的 descriptor。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- networking
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100