sbidy / pywizlight

A python connector for WiZ devices
MIT License
463 stars 79 forks source link

Handle Windows python's closure of broadcast socket #97

Closed UH-60 closed 2 years ago

UH-60 commented 2 years ago

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 commented 2 years ago

@all-contributors please add @UH-60 code

allcontributors[bot] commented 2 years ago

@sbidy

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