Fix NameError and socket leak in TCPClient._create_stream error path
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.
As the linter shows, returning the socket object is incorrect. The signature cannot be satisfied as written on this error path. We need to either make the IOStream Optional or perhaps just raise the exception directly (I'm not sure why this was written to encapsulate the exception in a Future).
pr_body.md looks like a leak from another branch and should not have been committed.
Please add a test that executes this error path. You can use mocks if necessary but mock out the OS-level thing that raises the OSError, don't just mock out the whole IOStream constructor. This will also help us understand the true cause of errors that might take this path.