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

Handle Windows python's closure of broadcast socket #97

Merged
merged 1 commit into from
Jan 29, 2022

Conversation

UH-60
Copy link

@UH-60 UH-60 commented Jan 26, 2022

Platform: Windows 10 64-bit
Python version: 3.9.10

Problem: Under Windows, the UDP broadcast socket is closed asynchronously and may not be closed by the time a second discovery is initiated. This occurs even though the SO_REUSEADDR socket option is specified. This appears to be unique to Windows - Linux-based environments operate normally.

Observed output:

Traceback (most recent call last):
  File "demo.py", line 48, in <module>
    main_loop.run_until_complete(main())
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "demo.py", line 44, in main
    await discovery.discover_lights()
  File "\pywizlight\discovery.py", line 129, in discover_lights
    discovered_IPs = await find_wizlights(broadcast_address=broadcast_space)
  File "\pywizlight\discovery.py", line 111, in find_wizlights
    transport, protocol = await loop.create_datagram_endpoint(
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 1363, in create_datagram_endpoint
    raise exceptions[0]
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 1347, in create_datagram_endpoint
    sock.bind(local_address)
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted   

Steps to reproduce this:

async def main():
    await discovery.discover_lights()
    await discovery.discover_lights()

main_loop = asyncio.get_event_loop()
main_loop.run_until_complete(main())

Proposed fix: Wait for Protocol::connection_lost() to be invoked before considering the socket closed

@sbidy
Copy link
Owner

sbidy commented Jan 29, 2022

@all-contributors please add @UH-60 code

@allcontributors
Copy link
Contributor

@sbidy

I've put up a pull request to add @UH-60! 🎉

@sbidy sbidy merged commit 7bcb523 into sbidy:master Jan 29, 2022
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.

None yet

2 participants