Repository navigation
fix(core): stop downloads and sources before closing the HTTP engine - #573
Merged
Merged
Conversation
Ketch.close() cancelled the executions, asked the torrent engine to stop and closed the shared HttpEngine at once, without waiting for any of it. Torrent sessions' `stopped` announces failed against the closed engine, and `ketch server`'s shutdown hook returned before the executions' final pause, store() checkpoint and record writes ran, so a graceful restart lost progress. close() now releases the engine in order, in the background: it joins the cancelled executions, awaits each source's new DownloadSource.stop() (the torrent source awaits its engine's stop), then closes the sources, the HttpEngine and the dispatchers it was given. KetchApi.shutdown(timeout) does the same and waits for it, at most 5 s by default. `ketch server`, `ketch mcp --standalone`, `ketch <url>` and the desktop app's quit (InstanceManager.shutdown) call it.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a5215b4674
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Contributor
A torrent engine that was still starting when stop() came was only closed asynchronously, so stop() could return while it still held its listener and port mappings: startEngine() now stops it under the mutex stop() waits for. ReleaseDownloader awaits Ketch.shutdown(), so its HTTP engine is closed once download() returns, as before.
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Field tests of #570 restarted
ketch serverwith SIGTERM and found thatKetch.close()waited for nothing:DownloadCoordinator.close()cancelled the executions and returned their job without joining it.TorrentDownloadSource.close()only askedKotlinTorrentEngineto shut down, asynchronously.HttpEnginestraight away.As a result, torrent sessions'
stoppedannounces failed with "HTTP engine is closed" within milliseconds of "Closing Ketch". The CLI's shutdown hook also returned before the cancelled executions'NonCancellablecleanup ran (the final pause, thestore()checkpoint and the record writes). v1receivedPayloadByteswent back about 8 MB across a graceful restart.What changes
Ketch.close()still pauses running tasks forShutdownstraight away, so their records stayDOWNLOADING. It then releases everything else in the background (fromDispatchers.Default), in this order:DownloadSource.stop()(default: no-op). Sources stop side by side throughSourceResolver.stop();HttpEngine;KetchDispatchers.Default.KetchApi.shutdown(timeout = 5.seconds): new; its default just callsclose().Ketch's version closes and then waits until the sources and theHttpEngineare released, at mosttimeout. The timeout is wall-clock time, also underrunTest. Whatever hasn't finished by then carries on in the background.TorrentDownloadSource.stop()refuses new engines, cancels the seed watchers asclose()did, and awaitsKotlinTorrentEngine.stop(). That's when seeding sessions close and announcestopped.close()afterwards only closes what is left.ketch serverandketch mcp --standaloneshutdown hooks andketch <url>callshutdown(). So does the desktop app's quit, through a newInstanceManager.shutdown()that closes remote devices and awaits the embedded one.close()semantics),docs/api.md,docs/architecture.md,docs/developers.mdanddocs/torrent.mdare updated. Thelibrary:apiABI dump is updated too.Torrent downloads already announce
stoppedfrom their execution's cleanup (removeTorrent). Joining the executions before the HTTP engine closes is what lets those announces through.stop()covers sessions that outlived their download, such as seeders.Tests
KetchCloseTest.shutdown_whileDownloading_savesTheFinalStateBeforeSourcesStopAndTheEngineCloses: a source that takes a while to stop saves a later checkpoint while it stops.shutdown()returns with that state saved, and the order is: checkpoint with HTTP open →stop()with HTTP open →close()→ engine closed.KetchCloseTest.shutdown_returnsAfterItsTimeout_andTheRestStopsLater.TorrentSourceStopTest:stop()returns only once the engine's stop does, and no engine starts after it.Ketch.shutdown()lets the engine'sstoppedannounce through a liveHttpEngine.InstanceManagerShutdownTest: the embedded device is awaited and remote ones are closed.ConfigurableNetworkHttpEngineTestnow usesshutdown(), because the engine is released afterclose()returns rather than inside it.Run locally, all passing:
:library:core:jvmTest,:library:torrent:jvmTest,:app:shared:jvmTest,:cli:test,:app:desktop:test,:library:server:test,:hooks:test,:library:mcp:test,:library:remote:jvmTest,:updater:test,:library:api:checkKotlinAbiand:library:core:compileTestKotlinIosSimulatorArm64. Core's close tests also pass onjsNodeTest. OnwasmWasiNodeTest, the tests that write to the temp folder crash inside okio, including the existingclose_whileDownloadingtest. CI doesn't run that target.Built on
main; independent of #570, which touches none of these lines.