python / python/cpython

create_unix_server() removes listening unix socket, potentially from other process

未關閉
#121,252 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

I made a mistake leading to reusing the same unix socket path for multiple Python processes, then as a surprise realized only the last process's unix socket listening, and none will be listening if the last process stopped.

I then realized this was implemented in https://github.com/python/asyncio/pull/441 for https://bugs.python.org/issue28399 which isn't clear on why deleting the original unix socket make sense. Assumingly it is for removing old unix socket files that's not in use, but apparently it can be removing listening sockets as well.

I don't think this should be intended behavior but open to any comment. Thanks.

To reproduce, with repro.py as below:

import os
import asyncio


class IdentityProtocol(asyncio.Protocol):
    """a protocol that politely replies server's pid then close connection"""

    def connection_made(self, transport):
        print('got new connection')
        transport.write(b'hello from pid=%d\r\n' % os.getpid())
        transport.close()


async def main():
    server = await asyncio.get_running_loop().create_unix_server(
            lambda: IdentityProtocol(), 'duplicatedpath.sock'
        )
    async with server:
        await server.serve_forever()


asyncio.run(main())

run two of such instances in separate terminals for the same working directory, and after each process created, use nc -CU duplicatedpath.sock which should simply show the pid of the last python process and exit.

CPython versions tested on:

3.10

Operating systems tested on:

Linux

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先在 Linux 上使用提供的 repro.py 和 nc 命令重現該行為,接著追蹤 asyncio.create_unix_server() 並檢視連結的 PR 441 和 issue 28399。當 socket 清理行為不再刪除其他程序正在監聽的 Unix socket,且回報的情境由回歸測試涵蓋時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
networking
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。