GetListing returns an incomplete listing without an error when the data connection is reset (control reply 226)

オープン
#1,887 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
networking

調査の方向性

The silent catch is in FluentFTP/Client/AsyncClient/GetListing.cs (around lines 410-434) and FluentFTP/Client/SyncClient/GetListing.cs (around lines 283-307), where the data-stream IOException is dropped and the partial lines are returned. Run the repro's Program.cs with dotnet run; it uses a scripted server on 127.0.0.1 and exits 1 while the defect is present. Done when the reset runs either throw or return all 4 entries for both clients, and the normal-close runs still return 4 entries.

索引モデルが issue の本文から書いたものです。

説明

FTP Server OS: n/a (scripted test server on 127.0.0.1, included below)

FTP Server Type: n/a (scripted test server)

Client Computer OS: Windows 11 x64

FluentFTP Version: 55.1.0 (NuGet); also master at cdc4c1f (built from source)

Framework: .NET 8 (8.0.34) and .NET 11 (11.0.0)

What happens

The data connection of a directory listing ends with a TCP reset (RST) after part of the listing was sent, and the control connection replies 226. GetListing then returns only what it read before the reset and does not throw:

  • FtpClient.GetListing("/") returns 2 of 4 entries.
  • AsyncFtpClient.GetListing("/") returns 0 of 4 entries.

The caller cannot tell the result is incomplete. Inside GetListing the reset is thrown as an IOException (inner SocketException, SocketErrorCode = ConnectionReset; visible with dotnet run -- --log), and it does not reach the caller. Reading the source, it ends in this catch block, which drops the exception and returns the lines read so far: AsyncClient/GetListing.cs#L410-L434, SyncClient/GetListing.cs#L283-L307. The control connection is still connected, so the retry/throw added for #1080 does not apply.

With the same server and a normal end of the data connection (FIN), both clients return all 4 entries.

Expected

GetListing reports the failed transfer (throws, or retries and returns the complete listing). It should not return a partial listing as if it were complete.

Minimal repro

Two files. The program starts a small scripted FTP server on 127.0.0.1 (EPSV + LIST), so no external server is needed. For each client it lists once with a normal end of the data connection (control run) and once with an RST after 2 of the 4 lines, with 226 on the control connection before the RST. Exit code 1 means the incomplete listing came back without an error.

FluentFtpListingReset.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentFTP" Version="55.1.0" />
  </ItemGroup>

</Project>
Program.cs
// Minimal repro: FluentFTP GetListing returns an incomplete listing and reports success when the DATA
// connection is reset (TCP RST) in the middle of the transfer while the control connection replies
// "226 transfer complete". A tiny scripted FTP server on 127.0.0.1 plays the server side.
//
//   dotnet run            -> prints one line per scenario; exit code 1 = defect reproduced
//   dotnet run -- --log   -> the same with FluentFTP's verbose log (Config.LogToConsole) for the reset runs
//                            and every IOException thrown inside the process (first-chance)
using System.Net;
using System.Net.Sockets;
using System.Text;
using FluentFTP;

string[] listing =
[
	"-rw-r--r-- 1 owner group 10 Jan 01 2026 a.txt",
	"-rw-r--r-- 1 owner group 10 Jan 01 2026 b.txt",
	"-rw-r--r-- 1 owner group 10 Jan 01 2026 c.txt",
	"-rw-r--r-- 1 owner group 10 Jan 01 2026 d.txt",
];
var log = args.Contains("--log");
if (log)
	AppDomain.CurrentDomain.FirstChanceException += (_, e) =>
	{
		if (e.Exception is IOException { InnerException: SocketException socket })
			Console.WriteLine($"[first-chance] IOException, inner SocketException {socket.SocketErrorCode}");
	};
var defect = false;
var stubBroken = false;

foreach (var async in new[] { false, true })
{
	var client = async ? "AsyncFtpClient" : "FtpClient     ";

	// control: the data connection is closed normally (FIN) after the whole listing
	var (count, error) = await ListAsync(async, StartServer(listing, resetAfterLines: null), log: false);
	Console.WriteLine($"{client} data connection closed normally          -> {Describe(count, error)}");
	stubBroken |= count != listing.Length;

	// defect: the data connection is reset (RST) after 2 of 4 lines; the control connection says 226
	(count, error) = await ListAsync(async, StartServer(listing, resetAfterLines: 2), log);
	var silent = error is null && count < listing.Length;
	Console.WriteLine($"{client} data connection reset after 2 of 4 lines -> {Describe(count, error)}{(silent ? "   <-- incomplete listing, no error" : "")}");
	defect |= silent;
}

return stubBroken ? 2 : defect ? 1 : 0;

static string Describe(int count, Exception? error) =>
	error is null ? $"{count} entries, no exception" : $"{error.GetType().Name}: {error.Message}";

static async Task<(int Count, Exception? Error)> ListAsync(bool async, int port, bool log)
{
	try
	{
		if (async)
		{
			await using var ftp = new AsyncFtpClient("127.0.0.1", "user", "pass", port);
			ftp.Config.LogToConsole = log;
			await ftp.Connect();
			return ((await ftp.GetListing("/")).Length, null);
		}
		else
		{
			using var ftp = new FtpClient("127.0.0.1", "user", "pass", port);
			ftp.Config.LogToConsole = log;
			ftp.Connect();
			return (ftp.GetListing("/").Length, null);
		}
	}
	catch (Exception ex)
	{
		return (-1, ex);
	}
}

// Scripted single-session FTP server. EPSV for the data connection, LIST for the listing.
static int StartServer(string[] listing, int? resetAfterLines)
{
	var control = new TcpListener(IPAddress.Loopback, 0);
	control.Start();
	new Thread(() => Serve(control, listing, resetAfterLines)) { IsBackground = true }.Start();
	return ((IPEndPoint)control.LocalEndpoint).Port;
}

static void Serve(TcpListener control, string[] listing, int? resetAfterLines)
{
	using var session = control.AcceptTcpClient();
	control.Stop();
	var stream = session.GetStream();
	var reader = new StreamReader(stream, Encoding.ASCII);
	void Reply(string text) => stream.Write(Encoding.ASCII.GetBytes(text + "\r\n"));
	TcpListener? passive = null;

	Reply("220 ready");
	while (reader.ReadLine() is { } command)
	{
		switch (command.Split(' ')[0].ToUpperInvariant())
		{
			case "USER": Reply("331 password required"); break;
			case "PASS": Reply("230 logged in"); break;
			case "SYST": Reply("215 UNIX Type: L8"); break;
			case "PWD": Reply("257 \"/\" is the current directory"); break;
			case "TYPE": Reply("200 type set"); break;
			case "EPSV":
				passive = new TcpListener(IPAddress.Loopback, 0);
				passive.Start();
				Reply($"229 Entering Extended Passive Mode (|||{((IPEndPoint)passive.LocalEndpoint).Port}|)");
				break;
			case "LIST":
				using (var data = passive!.AcceptSocket())
				{
					passive.Stop();
					Reply("150 opening data connection");
					var lines = resetAfterLines is { } n ? listing.Take(n) : listing;
					data.Send(Encoding.ASCII.GetBytes(string.Concat(lines.Select(l => l + "\r\n"))));
					if (resetAfterLines is null)
					{
						data.Shutdown(SocketShutdown.Both);       // normal end of the transfer (FIN) ...
						data.Close();
						Reply("226 transfer complete");           // ... then 226
					}
					else
					{
						Reply("226 transfer complete");           // 226 on the control connection ...
						data.LingerState = new LingerOption(true, 0);
						data.Close();                             // ... and the data connection ends with RST
					}
				}
				break;
			case "QUIT": Reply("221 bye"); return;
			default: Reply("502 not implemented"); break;
		}
	}
}

Output

The same output on 20 of 20 runs each with .NET 8 / FluentFTP 55.1.0, .NET 11 / FluentFTP 55.1.0, and .NET 8 / master cdc4c1f:

FtpClient      data connection closed normally          -> 4 entries, no exception
FtpClient      data connection reset after 2 of 4 lines -> 2 entries, no exception   <-- incomplete listing, no error
AsyncFtpClient data connection closed normally          -> 4 entries, no exception
AsyncFtpClient data connection reset after 2 of 4 lines -> 0 entries, no exception   <-- incomplete listing, no error

Logs :

dotnet run -- --log, FtpClient reset run (the [first-chance] line is printed by the repro, not by FluentFTP):

# Connect(False)
Status:   FluentFTP 55.1.0.0(Windows/.NET 8.0) FtpClient
Status:   Connecting(sync) FtpClient.FtpSocketStream(control) IP #1 = ***:57887
Status:   Waiting for a response
Response: 220 ready [739931,447d]
Command:  USER ***
Status:   Waiting for response to: USER ***
Response: 331 password required [<1ms]
Command:  PASS ***
Status:   Waiting for response to: PASS ***
Response: 230 logged in [<1ms]
Command:  FEAT
Status:   Waiting for response to: FEAT
Response: 502 not implemented [<1ms]
Status:   Text encoding: System.Text.ASCIIEncoding+ASCIIEncodingSealed
Command:  SYST
Status:   Waiting for response to: SYST
Response: 215 UNIX Type: L8 [<1ms]
Status:   Active ServerHandler is: None
Status:   Listing parser set to: Unix
Command:  PWD
Status:   Waiting for response to: PWD
Response: 257 "/" is the current directory [<1ms]

# GetListing("/", Auto)
Command:  TYPE I
Status:   Waiting for response to: TYPE I
Response: 200 type set [<1ms]

# OpenDataStream("LIST /", 0)

# OpenPassiveDataStream(AutoPassive, "LIST /", 0)
Command:  EPSV
Status:   Waiting for response to: EPSV
Response: 229 Entering Extended Passive Mode (|||57889|) [<1ms]
Status:   Connecting(sync) FtpClient.FtpSocketStream(data) IP #1 = ***:57889
Command:  LIST /
Status:   Waiting for response to: LIST /
Response: 150 opening data connection [<1ms]
+---------------------------------------+
Listing:  -rw-r--r-- 1 owner group 10 Jan 01 2026 a.txt
Listing:  -rw-r--r-- 1 owner group 10 Jan 01 2026 b.txt
[first-chance] IOException, inner SocketException ConnectionReset
Status:   Disposing(sync) FtpClient.FtpSocketStream(data)

# CloseDataStream()
Status:   Waiting for response to: LIST /
Response: 226 transfer complete [5ms]
Status:   Disposing(sync) FtpClient.FtpSocketStream(data) (redundant)
Status:   Confirmed format Unix

# Dispose()
Status:   Disposing(sync) FtpClient

# Disconnect()
Command:  QUIT
Status:   Waiting for response to: QUIT
Response: 221 bye [<1ms]
Status:   Disposing(sync) FtpClient.FtpSocketStream(control)

Related

  • #1080: GetListing returned an empty array when the control connection was lost during the listing. The fix retries once or throws when m_stream.IsConnected is false. In this repro only the data connection is reset; the control connection stays connected and the 226 is read normally.
主要言語
C#
スター
3.4k
フォーク
684
平均マージ
3日 11時間
マージ済み PR(30日)
4

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

robinrodricks/FluentFTP のほかの issue

robinrodricks/FluentFTP の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。