Skip to content

Stop the SFTP prefetch thread when its file is closed - #2719

Open
CAOShurong wants to merge 1 commit into
paramiko:mainfrom
CAOShurong:fix-sftp-prefetch-close
Open

CAOShurong wants to merge 1 commit into
paramiko:mainfrom
CAOShurong:fix-sftp-prefetch-close

Conversation

@CAOShurong

Copy link
Copy Markdown

Closing an SFTP file left a limited prefetch thread running. After close(), the thread kept sending CMD_READ. Closing the client then raised OSError: Socket is closed. When a later read failed, _async_response left that request in _prefetch_extents, the in-flight count stayed at the limit, and the thread slept until the process exited.

close() now joins the prefetch threads after the file is marked closed and before CMD_CLOSE. The thread returns from its wait loop once it sees the closed file, so it does not start another read. A request already inside _async_request is still sent; close() waits for that call to return. Requests already on the wire are left for the transport to finish.

_prefetch_done is unchanged. A fast reader can still empty the in-flight map before every chunk is queued, which is the race in #2519.

requireNonAsciiLocale() called pytest.skip() while the SFTP tests were imported. On a non-POSIX host that aborts collection of the whole module. Those locale tests are now skipped on their own.

inv test on Python 3.13.1 Windows: 366 passed, 13 skipped, 1 failed. The failure is test_new_keyfiles_avoid_descriptor_race_integration: the new key file's mode is 0o666 rather than 0o600. inv test --include-slow -k TestSFTP: 32 passed, 4 skipped, 3 failed (testa_posix_rename, test_symlink, test_chdir). test_prefetch_stops_when_file_closes passed. Those three failures are path and extension behavior in the in-process server on Windows.

Fixes #2717.

A limited prefetch kept issuing reads after close(), which raised
OSError once the channel was gone or slept forever when a failed
read left the in-flight count stuck. close() now joins that thread
before CMD_CLOSE.

Skip non-POSIX locale tests individually so importing the SFTP
module can finish on current pytest.
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] - With max_concurrent_prefetch_requests, the prefetch thread outlives the download

1 participant