Skip to content
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

use alternate port for CPython example #78

Merged
merged 1 commit into from
Feb 1, 2024

Conversation

tedder
Copy link
Contributor

@tedder tedder commented Feb 1, 2024

Follows up on #76, specifically @FoamyGuy's note that sudo is required.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Feb 1, 2024

Thank you @tedder

This version does run without sudo.

One other thing I noticed now is that after running the script and then stopping it, if I attempt to run it again this exception is raised:

❯ python httpserver_cpython.py 
Traceback (most recent call last):
  File "/home/timc/repos/circuitpython/Adafruit_CircuitPython_HTTPServer/examples/httpserver_cpython.py", line 23, in <module>
    server.serve_forever("127.0.0.1", 5000)
  File "/home/timc/.local/lib/python3.10/site-packages/adafruit_httpserver/server.py", line 185, in serve_forever
    self.start(host, port)
  File "/home/timc/.local/lib/python3.10/site-packages/adafruit_httpserver/server.py", line 213, in start
    self._sock.bind((host, port))
OSError: [Errno 98] Address already in use

In this case I had changed "0.0.0.0" to "127.0.0.1" after "using up" a few ports on "0.0.0.0" in order to see if they'd also be blocked with the 127 address. I was able to re-use the same port once on the 127 address, but then if I stopped and started again it would be in use error.

It seems that each pairing of IP + Port is getting held onto by something and is ineligible for re-use.

I'm not sure if it will help, but it might be worth trying the polling method rather than serve_forever(), perhaps that will be be better able to free the resources associated with the IP+Port.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Feb 1, 2024

It appears that the resources do get freed after a few minutes. I tried again after 2-3 minutes and it was able to run, but after stopping it goes back to having the error of address in use if you run again without waiting for 2-3 minutes.

If there is any way to modify the example code and/or library code to make it so these get freed whenever the script is stopped with KeyboardInterrupt I would be in favor of that.

@michalpokusa
Copy link
Contributor

In theory, that is what should be happening.
https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/blob/main/adafruit_httpserver/server.py#L191

The login in Server.stop() should be able to do just that by calling self._sock.close(), maybe there is something missing there.

@michalpokusa
Copy link
Contributor

michalpokusa commented Feb 1, 2024

https://docs.python.org/3/library/socket.html#socket.socket.close

close() releases the resource associated with a connection but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call shutdown() before close().

Maybe there should be a .shutdown() before .close()? I canot test right now but seems like this might be the issue.

@michalpokusa
Copy link
Contributor

I managed to test my first idea, turned out that it was not it, but this version might be the the solution:
https://github.com/michalpokusa/Adafruit_CircuitPython_HTTPServer/tree/cpython-fix

Works correctly on my machine, additional tests would be appreciated.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, and it is independent from the address in use thing which can be addressed in a future PR. Thanks again for this change @tedder.

@michalpokusa the branch you linked does seem to resolve the issue on my end as well. Go ahead and make a PR from that branch when you have a moment. Thank you for looking into it!

@FoamyGuy FoamyGuy merged commit af0a0bd into adafruit:main Feb 1, 2024
1 check passed
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Feb 6, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_DS18X20 to 1.4.0 from 1.3.19:
  > Merge pull request adafruit/Adafruit_CircuitPython_DS18X20#29 from ilikecake/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_FocalTouch to 1.5.2 from 1.5.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_FocalTouch#32 from ilikecake/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_Seesaw to 1.16.2 from 1.16.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_seesaw#129 from scirelli/issue_128

Updating https://github.com/adafruit/Adafruit_CircuitPython_Display_Button to 1.9.1 from 1.9.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Display_Button#44 from DJDevon3/DJDevon3_Working_Branch

Updating https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_Layout to 2.1.0 from 2.0.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_DisplayIO_Layout#92 from FoamyGuy/gridlayout_cell_contains

Updating https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer to 4.5.3 from 4.5.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#79 from michalpokusa/cpython-fix
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#78 from tedder/ted/cpython_port5k

Updating https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation to 2.9.0 from 2.8.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_LED_Animation#116 from tylerwinfield/main

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyCamera to 0.0.9 from 0.0.8:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyCamera#23 from adafruit/blendmode
  > Merge pull request adafruit/Adafruit_CircuitPython_PyCamera#22 from adafruit/timelapse
  > Merge pull request adafruit/Adafruit_CircuitPython_PyCamera#21 from adafruit/fix_rl

Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 2.0.5 from 2.0.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#152 from justmobilize/fix-null-headers

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
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 this pull request may close these issues.

3 participants