Skip to content

fix: connect to backend immediately on channel active for server-first protocols(#794)#813

Merged
jglogan merged 1 commit intoapple:mainfrom
Ronitsabhaya75:fix-tcp-port-forwarding
Dec 1, 2025
Merged

fix: connect to backend immediately on channel active for server-first protocols(#794)#813
jglogan merged 1 commit intoapple:mainfrom
Ronitsabhaya75:fix-tcp-port-forwarding

Conversation

@Ronitsabhaya75
Copy link
Contributor

@Ronitsabhaya75 Ronitsabhaya75 commented Oct 27, 2025

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

closes #794

TCP port forwarding currently fails for server-first protocols (SMTP, FTP, SSH, PostgreSQL) because the backend connection is only established when the client sends data.

Current behavior in ConnectHandler.swift:

func channelRead(context: ChannelHandlerContext, data: NIOAny) {
    if self.pendingBytes.isEmpty {
        self.connectToServer(context: context)  // Only connects on first data!
    }
}

This breaks any protocol where the server speaks first.

The fix:
Connect to the backend immediately when the channel becomes active, not when data arrives. This allows bidirectional communication from the start.

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

@Ronitsabhaya75 Ronitsabhaya75 force-pushed the fix-tcp-port-forwarding branch from 58d6c11 to 385f421 Compare October 27, 2025 23:13
@liufeng-xiwo
Copy link

The problem also breaks connecting mysql service with port forwarding.

@Ronitsabhaya75
Copy link
Contributor Author

The problem also breaks connecting mysql service with port forwarding.

can you go bit in detail please.

@Ronitsabhaya75 Ronitsabhaya75 force-pushed the fix-tcp-port-forwarding branch 2 times, most recently from d924d1f to 1ffc7e9 Compare November 3, 2025 13:59
@jglogan jglogan self-requested a review November 3, 2025 17:27
@jglogan
Copy link
Contributor

jglogan commented Dec 1, 2025

@Ronitsabhaya75 Thanks for the simplification+fix! Let's get this into the release.

@liufeng-xiwo I don't know whether I've seen this issue with mysql. If you still have a problem there, feel free to create another issue and provide the steps that would allow us to reproduce the problem you see.

@jglogan jglogan merged commit f394d87 into apple:main Dec 1, 2025
2 checks passed
saehejkang pushed a commit to saehejkang/container that referenced this pull request Jan 27, 2026
…t protocols(apple#794) (apple#813)

- Closes apple#794 
- TCP port forwarding currently fails for server-first protocols (SMTP,
  FTP, SSH, PostgreSQL) because the backend connection is only established
  when the client sends data.
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.

[Bug]: TCP Port Forwarding Fails: Connection succeeds but data stream times out

3 participants

Comments