Skip to content

Fix UDP server hanging on SIGINT due to untracked background tasks#1

Closed
shugonta with Copilot wants to merge 3 commits into
claude/udp-fps-input-01WpGQXHF6hgWmSr39WrmQcifrom
copilot/fix-udp-server-ctrl-c-issue
Closed

Fix UDP server hanging on SIGINT due to untracked background tasks#1
shugonta with Copilot wants to merge 3 commits into
claude/udp-fps-input-01WpGQXHF6hgWmSr39WrmQcifrom
copilot/fix-udp-server-ctrl-c-issue

Conversation

Copilot AI commented Dec 5, 2025

Copy link
Copy Markdown

UDP server would not terminate with Ctrl+C because asyncio.create_task() calls in the datagram handler created untracked tasks that prevented clean shutdown.

Changes

  • Track background tasks: Added background_tasks set to UDPInputServer with automatic cleanup via task completion callback
  • Enhanced shutdown: stop() now cancels pending tasks, waits for completion, and allows transport flush time before closing
# Before: fire-and-forget task creation
def datagram_received(self, data: bytes, addr: tuple):
    asyncio.create_task(self.server.handle_packet(data, addr))

# After: tracked with automatic cleanup
def datagram_received(self, data: bytes, addr: tuple):
    task = asyncio.create_task(self.server.handle_packet(data, addr))
    self.server.background_tasks.add(task)
    task.add_done_callback(lambda t: self.server.background_tasks.discard(t))
# Enhanced stop() method
async def stop(self):
    self.running = False
    
    # Cancel and wait for background tasks
    for task in self.background_tasks:
        task.cancel()
    if self.background_tasks:
        await asyncio.gather(*self.background_tasks, return_exceptions=True)
    
    # Close transport with flush time
    if self.transport:
        self.transport.close()
        await asyncio.sleep(0.1)
        self.transport = None

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: /usr/bin/curl curl -LsSf REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

I can not finish the udp server with ctrl-c

Created from VS Code via the GitHub Pull Request extension.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 5, 2025 10:16
Co-authored-by: shugonta <827026+shugonta@users.noreply.github.com>
Co-authored-by: shugonta <827026+shugonta@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix UDP server shutdown issue with ctrl-c Fix UDP server hanging on SIGINT due to untracked background tasks Dec 5, 2025
Copilot AI requested a review from shugonta December 5, 2025 10:23
@shugonta

shugonta commented Dec 5, 2025

Copy link
Copy Markdown
Owner

@copilot How did you change udp_server.py

Copilot AI commented Dec 5, 2025

Copy link
Copy Markdown
Author

@copilot How did you change udp_server.py

I made three changes to server/udp_server.py:

  1. Line 100 - Added background_tasks set to track async tasks:

    self.background_tasks: Set[asyncio.Task] = set()
  2. Lines 54-56 - Modified datagram_received() to track tasks and auto-cleanup:

    task = asyncio.create_task(self.server.handle_packet(data, addr))
    self.server.background_tasks.add(task)
    task.add_done_callback(lambda t: self.server.background_tasks.discard(t))
  3. Lines 142-149 - Enhanced stop() to cancel tasks before closing:

    # Cancel all background tasks
    for task in self.background_tasks:
        task.cancel()
    
    # Wait for all background tasks to complete
    if self.background_tasks:
        await asyncio.gather(*self.background_tasks, return_exceptions=True)
    
    # Close transport with 0.1s flush time

These changes ensure background tasks don't prevent clean shutdown when Ctrl+C is pressed.

@shugonta shugonta closed this Dec 5, 2025
@shugonta shugonta deleted the copilot/fix-udp-server-ctrl-c-issue branch December 8, 2025 06:03
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.

2 participants