簡體   English   中英

異步傳輸未關閉 TCP 連接

[英]asyncio Transport does not close TCP connection

所以我對 asyncio 有點問題。 參考代碼為:

總之:

超時設置如下

    def _reset_timeout(self, duration=None):
        if self._timeout_handle is not None:
            self._timeout_handle.cancel()
        self._timeout_handle = self.loop.call_later(
            duration or self._timeout_duration, self._timeout_cb
        )

self._timeout_cb是這樣的:

    def _timeout_cb(self):
        log.info('%r connection timeout', self.session.peer)
        self.transport.close()

_reset_timeout在這部分被調用,像這樣:

            self._reset_timeout(self._proxy_timeout)

測試是這樣的:

class TestProxyProtocolV1Controller:
    def test_timeout(self, plain_controller):
        # The next line ensures that _proxy_timeout is set
        assert plain_controller.smtpd._proxy_timeout > 0.0
        prox_test = b"PROXY TCP4 255.255.255.255 255.255.255.255 65535 65535\r\n"
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            sock.connect(Global.SrvAddr)
            # The next line simulates timeout
            time.sleep(plain_controller.smtpd._proxy_timeout * 1.1)
            with pytest.raises(ConnectionAbortedError):
                sock.send(prox_test)
                _ = sock.recv(4096)

此測試適用於 Windows,但在 Linux 中失敗 失敗是這樣的:

aiosmtpd/tests/test_proxyprotocol.py:632: in test_timeout
    _ = sock.recv(4096)
E   Failed: DID NOT RAISE <class 'ConnectionAbortedError'>

如果我在測試中刪除pytest.raises部分,它可以在 Linux 中工作......但這意味着超時不會結束連接,這是不正確的!

所以,本質上,我的想法是:

self.transport.close()結束了 Windows 中的 TCP 連接,但不知何故使 TCP 連接在 ZEDC9F0A5A5D577497BF3168 中保持活動狀態

如何使 TCP 連接在 Linux 上結束? 除了self.transport.close()我還應該做什么?

我決定發布我實施的解決方法。

因此,我沒有在服務器代碼中進行處理,而是像這樣編輯了測試用例:

class TestProxyProtocolV1Controller:
    def test_timeout(self, plain_controller):
        # The next line ensures that _proxy_timeout is set
        assert plain_controller.smtpd._proxy_timeout > 0.0
        prox_test = b"PROXY TCP4 255.255.255.255 255.255.255.255 65535 65535\r\n"
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
            sock.connect(Global.SrvAddr)
            # The next line simulates timeout
            time.sleep(plain_controller.smtpd._proxy_timeout * 1.1)
            with pytest.raises(ConnectionAbortedError):
                sock.send(prox_test)
                resp = sock.recv(4096)
                if resp == b"":
                    raise ConnectionAbortedError

我對此完全不滿意,因此我將不勝感激任何建議/更正。

但無論如何,發展至少可以暫時繼續下去。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM