Skip to content

Socket gets stuck in __exit__ #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
e28eta opened this issue Jan 21, 2023 · 1 comment · Fixed by #97
Closed

Socket gets stuck in __exit__ #89

e28eta opened this issue Jan 21, 2023 · 1 comment · Fixed by #97

Comments

@e28eta
Copy link
Contributor

e28eta commented Jan 21, 2023

Working on what I thought would be a simple re-arrangment of examples/wiznet5k_simpleserver.py to make it a little easier to use (#88 ), I think I found one or more issues.

  1. I try sending the contents of this repo's code of conduct, using: nc 192.168.10.1 50007 < CODE_OF_CONDUCT.md
  2. The simple server reads 1024 bytes, echoes it back
  3. nc exits, which tells me that the TCP socket is closed: it knows it has nothing left to send, and I believe this indicates the server has closed their end too

Expected behavior:
Simple Server loops back and accepts another connection (per local changes in #88)

Actual behavior:
Server gets stuck inside __exit__

Connecting to the serial port on my RP2040 Feather, I can send control-c to stop the loop. The stack trace is:

Traceback (most recent call last):
  File "code.py", line 39, in <module>
  File "adafruit_wiznet5k/adafruit_wiznet5k_socket.py", line 184, in __exit__
  File "adafruit_wiznet5k/adafruit_wiznet5k_socket.py", line 207, in status
  File "adafruit_wiznet5k/adafruit_wiznet5k.py", line 666, in socket_status
  File "adafruit_wiznet5k/adafruit_wiznet5k.py", line 1126, in _read_snsr
  File "adafruit_wiznet5k/adafruit_wiznet5k.py", line 1166, in _read_socket
  File "adafruit_wiznet5k/adafruit_wiznet5k.py", line 573, in read
KeyboardInterrupt: 

I think it's stuck inside this loop waiting for the socket to transition out of FIN_WAIT:

def __exit__(self, exc_type, exc_val, exc_tb) -> None:
if self._sock_type == SOCK_STREAM:
self.disconnect()
stamp = time.monotonic()
while self.status == wiznet5k.adafruit_wiznet5k.SNSR_SOCK_FIN_WAIT:
if time.monotonic() - stamp > 1000:
raise RuntimeError("Failed to disconnect socket")

My hypothesis is that the socket isn't being closed because there's buffered data waiting to be read, but I don't know that for sure. The simple server example code seems fragile for requests under 1024 bytes (blocks waiting for more, I think the default is 0 timeout), and I'm hitting this issue with requests >1024 bytes.

On my client machine, netstat shows sockets to the ethernet module in both FIN_WAIT_1 and CLOSING status. I think one set of those are sockets where I canceled the nc and the server never set up a socket with accept() (so a red herring), but the other group is sockets for this issue.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Jan 23, 2023

I don't know if it's related to the issues you've mentioned, but this line jumps out as odd to me:

if time.monotonic() - stamp > 1000: 

I think that time.monotinc() returns a value in seconds so as written it seems like this would wait for 1000 seconds. Assume it was meant to be 1000ms or 1 second. I'll try to verify this.

I see this a separate issue now. Sorry to muddy this one up :x

@tekktrik tekktrik linked a pull request Feb 17, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants