Skip to content

TCP_client.close() does not finish properly even though TCP connection gets terminated correctly #4182

@20kaushik02

Description

@20kaushik02

Brief description

When using TCP_client to establish TCP connections, closing the connection does not work properly, and throws the following error: ImportError: sys.meta_path is None, Python is likely shutting down

Seems like this issue is very similar to this resolved issue at Selenium. So it would probably have a very similar fix, and I'd be willing to work on this if needed.

Scapy version

2.5.0

Python version

3.10.5

Operating system

Windows 11 22H2

Additional environment information

No response

How to reproduce

This example script makes an HTTP request over the TCP connection.

from scapy.layers.http import HTTP, HTTPRequest
from scapy.layers.inet import TCP_client

# Establish TCP connection
tcp_conn = TCP_client.tcplink(HTTP, "httpforever.com", 80)

# Craft HTTP request
test_req = HTTP() / HTTPRequest(
	Accept_Encoding=b'gzip, deflate',
	Cache_Control=b'no-cache',
	Connection=b'keep-alive',
	Host=b'httpforever.com'
)

# Send the crafted packet and receive the response
response = tcp_conn.sr1(test_req)
print(response.summary())

# Close the connection
tcp_conn.close()

Actual result

Packet transmission works, connection termination succeeds as well:

image

However, when exiting, the cleanup function __del__ throws the following error:

Begin emission:
Finished sending 1 packets.
*
Received 1 packets, got 1 answers, remaining 0 packets
HTTP / 'HTTP/1.1' '200' 'OK' / Raw
Exception ignored in: <function Automaton.__del__ at 0x000001CB831F04C0>
Traceback (most recent call last):
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\site-packages\scapy\automaton.py", line 1049, in __del__
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\site-packages\scapy\automaton.py", line 1354, in stop
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\site-packages\scapy\automaton.py", line 182, in send
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\site-packages\scapy\automaton.py", line 160, in _winset
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\site-packages\scapy\error.py", line 150, in warning
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\logging\__init__.py", line 1489, in warning
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\logging\__init__.py", line 1624, in _log
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\logging\__init__.py", line 1633, in handle
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\logging\__init__.py", line 821, in filter
  File "C:\Users\rknar\.pyenv\pyenv-win\versions\3.10.5\lib\site-packages\scapy\error.py", line 52, in filter
ImportError: sys.meta_path is None, Python is likely shutting down

Expected result

Same output, no error, object cleanup happens correctly.

Related resources

Related issue: SeleniumHQ/selenium#3330

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions