Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

ForwardedPortLocal: a local client that stops reading stalls every channel of the session #1874

Backlog

Description

@richardbeyerschroeder

Version: 2026.0.0 (same code on develop as of 2026-11-26).

What happens. Session.MessageListener delivers channel data inline, and ChannelDirectTcpip.OnData writes it to the
local socket with a synchronous Send. When the local client of one forwarded connection stops reading, its socket
buffer fills and the listener blocks in that Send. The SSH socket is then no longer read, so no other channel of the
session makes progress.

Channel.OnData also sends the window adjust before the data reaches the local socket, so per-channel flow control never
throttles the server; only TCP on the shared connection does.

How we hit it. A database client stops reading a large result early and cancels the query. The driver
(MySqlConnector 2.6.1) cancels over a second connection to the same forwarded port and waits for its reply, which is
queued behind the first connection's unread data. Deadlock. Reproduced against MySQL 8.4 behind an OpenSSH bastion, on
Linux and Windows; it is timing-dependent.

Stacks at the hang:

Renci.SshNet.Session.MessageListener
  Renci.SshNet.Channels.Channel.OnChannelData
    Renci.SshNet.Channels.ChannelDirectTcpip.OnData
      Renci.SshNet.Abstractions.SocketAbstraction.Send
        System.Net.Sockets.Socket.Send

MySqlConnector.MySqlCommand.Cancel
  ... MySqlConnector.Core.ServerSession.InitSslAsync
    System.Net.Security.SslStream.AuthenticateAsClient
      System.Net.Sockets.Socket.Receive

Minimal reproduction (no database involved; hangs every time on Windows and on Linux, .NET 10).

docker-compose.yml, an OpenSSH bastion plus two targets reachable only through it:

services:
  bastion:
    image: alpine:3.20
    command: >
      sh -c "apk add --no-cache openssh > /dev/null && ssh-keygen -A > /dev/null &&
             adduser -D tester && echo tester:tester | chpasswd &&
             exec /usr/sbin/sshd -D -e -o PasswordAuthentication=yes -o AllowTcpForwarding=yes"
    ports:
      - "127.0.0.1:2223:22"
    networks: [net]

  # Sends data forever to whoever connects.
  streamer:
    image: alpine:3.20
    command: sh -c "while true; do yes | nc -l -p 9000; done"
    networks: [net]

  # Sends one line as soon as someone connects.
  greeter:
    image: alpine:3.20
    command: sh -c "while true; do echo hello | nc -l -p 9001; done"
    networks: [net]

networks:
  net: {}

repro.cs, run with dotnet run repro.cs (.NET 10 file-based app):

#:package SSH.NET@2026.0.0

using System.Net;
using System.Net.Sockets;
using Renci.SshNet;

using var client = new SshClient("127.0.0.1", 2223, "tester", "tester");
client.Connect();

var toStreamer = new ForwardedPortLocal("127.0.0.1", 19000, "streamer", 9000);
var toGreeter = new ForwardedPortLocal("127.0.0.1", 19001, "greeter", 9001);
client.AddForwardedPort(toStreamer);
client.AddForwardedPort(toGreeter);
toStreamer.Start();
toGreeter.Start();

Console.WriteLine($"Greeter, nothing stalled:        {ReadGreeting()}");

// Connect through the first forward, wait until data is flowing, then stop reading it.
using var stalled = new TcpClient();
stalled.Connect(IPAddress.Loopback, 19000);
SpinWait.SpinUntil(() => stalled.Available > 0, TimeSpan.FromSeconds(10));
Thread.Sleep(TimeSpan.FromSeconds(3));

Console.WriteLine($"Greeter, other channel stalled:  {ReadGreeting()}");

static string ReadGreeting()
{
    using var greeter = new TcpClient();
    greeter.Connect(IPAddress.Loopback, 19001);
    greeter.ReceiveTimeout = 10_000;
    try
    {
        return new StreamReader(greeter.GetStream()).ReadLine() ?? "(closed)";
    }
    catch (IOException)
    {
        return "no reply within 10 s";
    }
}

Output:

Greeter, nothing stalled:        hello
Greeter, other channel stalled:  no reply within 10 s

Reading the first connection in the background instead (stalled.GetStream().CopyTo(Stream.Null)) makes the second line
print hello, so the stall comes from the unread channel.

Expected. A stalled consumer of one forwarded connection throttles only its own channel.

Possible fix. Deliver data to the local socket off the listener thread, and send the window adjust only after the
local consumer has taken the data.

Possibly related: #1858 (a different stall in ForwardedPortLocal).

AI disclosure: investigated and drafted with Claude (Anthropic). I reviewed and edited the text and ran the reproduction
myself.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions