Skip to content

Conversation

@jonlunsford
Copy link

Summary

Fixes two bugs that prevented the TCP proxy from forwarding connections:

  1. Incorrect PID capture: self() was called inside spawn_link, capturing the spawned accept_loop's PID instead of the GenServer's PID. This caused GenServer.cast to send messages to the wrong process.

  2. Missing socket ownership transfer: When accept_loop accepts a socket, it becomes the controlling process. Before the GenServer can transfer ownership to the handler process, it must first own the socket itself.

Changes

  • Capture GenServer PID before spawning accept_loop
  • Transfer socket ownership from accept_loop to GenServer before casting

Testing

Added regression tests that verify:

  • Socket ownership transfer allows handler processes to receive data in active mode
  • The GenServer properly receives new_connection casts
  • Multiple concurrent connections are handled correctly

Two bugs prevented the TCP proxy from forwarding connections:

1. Incorrect PID capture: self() was called inside spawn_link, capturing
   the spawned accept_loop's PID instead of the GenServer's PID. This caused
   GenServer.cast to send messages to the wrong process.

2. Missing socket ownership transfer: When accept_loop accepts a socket,
   it becomes the controlling process. Before the GenServer can transfer
   ownership to the handler process, it must first own the socket itself.

The fix captures the GenServer PID before spawning and transfers socket
ownership from accept_loop to GenServer before casting the new_connection
message.
@jonlunsford jonlunsford force-pushed the fix-proxy-socket-ownership branch from 9c2cde6 to 97b537e Compare January 25, 2026 06:36
The previous fix transferred socket ownership to the GenServer, but then
the spawned handler process couldn't receive data because it wasn't the
controlling process.

This fix:
1. Spawns the handler process first
2. Transfers socket ownership from GenServer to the handler
3. Signals the handler to proceed once ownership is transferred
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