Skip to content

Comments

Fix NameError and socket leak in TCPClient._create_stream error path#3579

Open
bysiber wants to merge 1 commit intotornadoweb:masterfrom
bysiber:fix/tcpclient-oserror-handling
Open

Fix NameError and socket leak in TCPClient._create_stream error path#3579
bysiber wants to merge 1 commit intotornadoweb:masterfrom
bysiber:fix/tcpclient-oserror-handling

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

In TCPClient._create_stream, if IOStream() constructor raises OSError, the except handler references stream which was never assigned:

try:
    stream = IOStream(socket_obj, max_buffer_size=max_buffer_size)
except OSError as e:
    fu = Future()
    fu.set_exception(e)
    return stream, fu  # NameError: 'stream' is not defined

This causes a NameError that masks the original OSError, making it impossible to diagnose the root cause from the traceback.

Additionally the socket object would be leaked since it's never closed in the error path.

Fixed by returning the socket_obj instead of the undefined stream variable, and closing the socket before returning.

If IOStream() constructor raises OSError, the except handler
references 'stream' which was never assigned, causing a NameError
that masks the original error. Also close the socket to prevent
a resource leak.
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.

1 participant