robinrodricks / FluentFTP

An FTP and FTPS client for .NET & .NET Standard, optimized for speed. Provides extensive FTP commands, File uploads/downloads, SSL/TLS connections, Automatic directory listing parsing, File hashing/checksums, File permissions/CHMOD, FTP proxies, FXP support, UTF-8 support, Async/await support, Powershell support and more. Written entirely in C#.
MIT License
3.13k stars 655 forks source link

timeout exception after download and upload all bytes #1497

Closed adfansong closed 8 months ago

adfansong commented 8 months ago

FTP Server OS: Windows

FTP Server Type: Windows Server IIS / FileZilla

Client Computer OS: Windows / iOS

FluentFTP Version: 49.0.2.0

Framework: Mono netstd2.1 / net462

I used AsyncFtpClient to DownloadFile and UploadFile async. After all bytes finished, it keeps waiting for response until timeout.

Logs :


[Verbose] >         Connect(False)

[Verbose] Status:   FluentFTP 49.0.2.0(.NET Standard 2.1) AsyncFtpClient

[Verbose] Status:   Waiting for a response

[Verbose] Response: 220-FileZilla Server 1.8.1

[Verbose] Status:   Waiting for response to: USER ***

[Verbose] Status:   Waiting for response to: PASS ***

[Verbose] Status:   Waiting for response to: FEAT

[Verbose] Response: 211-Features:
Response: MDTM
Response: REST STREAM
Response: SIZE
Response: MLST type*;size*;modify*;perm*;
Response: MLSD
Response: AUTH SSL
Response: AUTH TLS
Response: PROT
Response: PBSZ
Response: UTF8
Response: TVFS
Response: EPSV
Response: EPRT
Response: MFMT

[Verbose] Status:   Waiting for response to: OPTS UTF8 ON

[Verbose] Status:   Waiting for response to: SYST

[Verbose] Status:   Active ServerHandler is: FileZilla

[Verbose] Status:   Listing parser set to: Machine

[Verbose] Status:   Waiting for response to: PWD

[Verbose] >         GetListing("/", SizeModify)

[Verbose] Status:   Waiting for response to: TYPE I

[Verbose] >         OpenDataStreamAsync("MLSD /", 0)

[Verbose] >         OpenPassiveDataStreamAsync(AutoPassive, "MLSD /", 0)

[Verbose] Status:   Waiting for response to: EPSV

[Verbose] Status:   Waiting for response to: MLSD /

[Verbose] +---------------------------------------+

[Verbose] Listing:  type=dir;modify=20240229071657.936;perms=cplem; 123

[Verbose] Listing:  type=file;size=346;modify=20240228125057.157;perms=awr; 2.dzip

[Verbose] Listing:  type=file;size=9630587;modify=20240221030937.816;perms=awr; App使用教程2.zip

[Verbose] Listing:  type=file;size=566023426;modify=20240227091230.360;perms=awr; leapbox-4.6.4-avm.apk

[Verbose] Listing:  type=file;size=44414497;modify=20240226034038.410;perms=awr; test.apk

[Verbose] -----------------------------------------

[Verbose] Status:   Disposing(async) FtpSocketStream(data connection of AsyncFtpClient)

[Verbose] >         CloseDataStream()

[Verbose] Status:   Waiting for response to: MLSD /

[Verbose] >         DownloadFile("C:/Users/Administrator/AppData/LocalLow/bw/解压投屏助手\Root\2.dzip", "/2.dzip", Resume, None)

[Verbose] >         GetFileSize("/2.dzip", -1)

[Verbose] Status:   Waiting for response to: SIZE /2.dzip

[Verbose] >         OpenRead("/2.dzip", Binary, 0, 346, False)

[Verbose] >         OpenDataStreamAsync("RETR /2.dzip", 0)

[Verbose] >         OpenPassiveDataStreamAsync(AutoPassive, "RETR /2.dzip", 0)

[Verbose] Status:   Waiting for response to: EPSV

[Verbose] Status:   Waiting for response to: RETR /2.dzip

[Verbose] Status:   Downloaded 346 bytes

[Verbose] Status:   AsyncFtpClient incorrectly called Close(sync)

[Verbose] Status:   Disposing(sync) FtpSocketStream(data connection of AsyncFtpClient)

[Verbose] >         CloseDataStream()

[Verbose] Status:   Waiting for response to: RETR /2.dzip

[Verbose] Status:   Waiting for response to: RETR /2.dzip

[Verbose] Status:   Disposing(async) FtpSocketStream(control connection of AsyncFtpClient)

[Verbose] Status:   AsyncFtpClient incorrectly called Close(sync)
FanDjango commented 8 months ago

I need to see a complete log, (nearly) all the responses are missing (or are there none?).

[Verbose] Status:   Waiting for response to: RETR /2.dzip

Why does this appear 3 times? Need to see your source code.

What is the server type? FileZilla or IIS? Which version FileZilla?

Using Mono: always a bit difficult to help with that. Please supply more information.

FanDjango commented 8 months ago

Oh, and try using passive FTP mode instead of ACTIVE mode.

adfansong commented 8 months ago

I need to see a complete log, (nearly) all the responses are missing (or are there none?).

[Verbose] Status:   Waiting for response to: RETR /2.dzip

Why does this appear 3 times? Need to see your source code.

What is the server type? FileZilla or IIS? Which version FileZilla?

Using Mono: always a bit difficult to help with that. Please supply more information.

Thanks for your reply.

  1. I wrote a simple test to get logs and stacks.
    
    [Verbose] >         Connect(False)

[Verbose] Status: FluentFTP 49.0.2.0(.NET Standard 2.1) AsyncFtpClient

[Info] Status: Connecting to IP #1= ***:21

[Verbose] Status: Waiting for a response

[Verbose] Response: 220-FileZilla Server 1.8.1

[Info] Response: 220 Please visit https://filezilla-project.org/ [738944.456d]

[Info] Status: Detected FTP server: FileZilla

[Info] Command: USER ***

[Verbose] Status: Waiting for response to: USER ***

[Info] Response: 331 Please, specify the password. [109ms]

[Info] Command: PASS ***

[Verbose] Status: Waiting for response to: PASS ***

[Info] Response: 230 Login successful. [139ms]

[Info] Command: FEAT

[Verbose] Status: Waiting for response to: FEAT

[Verbose] Response: 211-Features: Response: MDTM Response: REST STREAM Response: SIZE Response: MLST type;size;modify;perm; Response: MLSD Response: AUTH SSL Response: AUTH TLS Response: PROT Response: PBSZ Response: UTF8 Response: TVFS Response: EPSV Response: EPRT Response: MFMT

[Info] Response: 211 End [444ms]

[Info] Status: Text encoding: System.Text.UTF8Encoding

[Info] Command: OPTS UTF8 ON

[Verbose] Status: Waiting for response to: OPTS UTF8 ON

[Info] Response: 202 UTF8 mode is always enabled. No need to send this command [187ms]

[Info] Command: SYST

[Verbose] Status: Waiting for response to: SYST

[Info] Response: 215 UNIX emulated by FileZilla. [81ms]

[Verbose] Status: Active ServerHandler is: FileZilla

[Verbose] Status: Listing parser set to: Machine

[Info] Command: PWD

[Verbose] Status: Waiting for response to: PWD

[Info] Response: 257 "/" is current directory. [75ms]

connected.

[Verbose] > DownloadFile("e://soft/download/1.dzip", "/1.dzip", Resume, None)

[Verbose] > OpenRead("/1.dzip", Binary, 0, 0, False)

[Verbose] > GetFileSize("/1.dzip", -1)

[Info] Command: SIZE /1.dzip

[Verbose] Status: Waiting for response to: SIZE /1.dzip

[Info] Response: 213 346 [23ms]

[Info] Command: TYPE I

[Verbose] Status: Waiting for response to: TYPE I

[Info] Response: 200 Type set to I [50ms]

[Verbose] > OpenDataStreamAsync("RETR /1.dzip", 0)

[Verbose] > OpenPassiveDataStreamAsync(AutoPassive, "RETR /1.dzip", 0)

[Info] Command: EPSV

[Verbose] Status: Waiting for response to: EPSV

[Info] Response: 229 Entering Extended Passive Mode (|||11382|) [139ms]

[Info] Status: Connecting to IP #1= ***:11382

[Info] Command: RETR /1.dzip

[Verbose] Status: Waiting for response to: RETR /1.dzip

[Info] Response: 150 Starting data transfer. [72ms]

[Verbose] Status: Downloaded 346 bytes

[Verbose] Status: AsyncFtpClient incorrectly called Close(sync)

[Verbose] Status: Disposing(sync) FtpSocketStream(data connection of AsyncFtpClient)

[Verbose] > CloseDataStream()

[Verbose] Status: Waiting for response to: RETR /1.dzip

[Info] Response: 226 Operation successful [93ms]

[Verbose] Status: Waiting for response to: RETR /1.dzip

[Verbose] Status: Disposing(async) FtpSocketStream(control connection of AsyncFtpClient)

[Verbose] Status: AsyncFtpClient incorrectly called Close(sync)

TimeoutException: Timed out trying to read data from the socket stream!


[stack.txt](https://github.com/robinrodricks/FluentFTP/files/14446891/stack.txt)

2.
The test code:

using System; using System.Collections; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using FluentFTP; using UnityEngine;

public class FluentFTPTest : MonoBehaviour { public string host = "192.168.0.103"; public string user = "test"; public string pwd = "123"; public int port = 0;

private AsyncFtpClient client;

// Start is called before the first frame update
void Start() {
    testCo();
}

async Task testCo() {
    try {
        var tokenSource = new CancellationTokenSource();

        var client = new AsyncFtpClient(host, user, pwd, port);
        // TEST
        client.Config.LogToConsole = true;
        client.Config.DataConnectionType = FtpDataConnectionType.AutoPassive;
        client.LegacyLogger = (level, s) => {
            Debug.Log($"[{level}] {s}");
        };

        await client.Connect(tokenSource.Token);

        Debug.Log("connected.");

        await client.DownloadFile("e://soft/download/1.dzip", "/1.dzip", token:tokenSource.Token);

        Debug.Log("done.");
    } catch (Exception e) {
        Debug.LogException(e);
        throw;
    }
}

}



for 3 times:
1) FtpSocketStream/<WriteAsync>
2) .AsyncFtpClient/<DownloadFileInternalAsync> -> FtpDataStream:Close
3) .AsyncFtpClient/<DownloadFileInternalAsync> -> GetReplyInternal

3. Now I use FileZilla Server 1.8.1, IIS the same before
adfansong commented 8 months ago

Oh, and try using passive FTP mode instead of ACTIVE mode.

How to set that?

I tried to set DataConnectionType to AutoPassive / PASV, same error.

FanDjango commented 8 months ago

Ok, I see the source code.

And I made a mistake concerning ACTIVE/PASSIVE, I was in a hurry. It is PASSIVE (see the EPSV, means extended passive mode). My bad.

FanDjango commented 8 months ago

I am able to reproduce the problem and am looking for a fix. Thanks for reporting this!

FanDjango commented 8 months ago

@adfansong I merged a fix for this and perhaps you can check it out on the current master?

adfansong commented 8 months ago

@adfansong I merged a fix for this and perhaps you can check it out on the current master?

Thanks for your quick support, it's ok now!