Repository navigation
fix(api): close the client on every path so tg api --raw exits - #71
Merged
Merged
Conversation
tg api --raw printed the result with console.log and returned. Only success() and error() exit the process, so mtcute's open connection kept the event loop alive and the command never finished: scripts that used --raw hung until killed. tg api now destroys the client after the call on every path (raw, the success wrapper and a failed call), which also saves the session, and --raw prints through a new raw() output helper that exits the way success() does. A fake client that holds the process open until destroy() covers all three paths in a new e2e test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…osing The fake destroy() printed its marker before its first await, so the api e2e test still passed if tg api called closeClient() without awaiting it and exited straight after. Real mtcute destroy() awaits the network and storage shutdown, which an early exit would cut short. The fake now sleeps before it clears the connection and prints the marker, so a missing await fails all three tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The simplified sample in docs/api-design.md still showed the --raw path that caused #69: it printed with console.log and returned, without closing the client, so the process never exited. Someone who copied it into a new command would bring the hang back. The sample now matches src/commands/api.ts: it closes the client on every path and prints through raw(), success() or error(), which exit the process. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The fake's destroy() cleared its only handle, so tg api exited on its own once the client was closed. Dropping the explicit process.exit from raw() still passed every api test, though that exit is what ends --raw if a connected mtcute client leaves something open after destroy(), which can't be checked offline. The fake's handle now outlives destroy(), so the process ends only through an explicit exit: --raw without it times out, and an unawaited close still fails all three tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What and why
tg api <method> --rawprinted the result and then hung until it was killed. The--rawpath printed withconsole.logand returned. Onlysuccess()anderror()exit the process, so mtcute's open connection kept the event loop alive. Without--rawand on a failed call,success()anderror()already exited the process, so those paths finished as expected.Fixes #69
Fix
--rawnow prints through a newraw()helper insrc/utils/output.ts, which exits the waysuccess()does. This is the fix users see. With the client closed, the process could exit on its own. But no command in the codebase relies on that (the daemon also callsprocess.exitafter it closes its clients), and this can't be checked offline against a connected mtcute client. So--rawexits explicitly, like every other output path.tg apialso closes the client (client.destroy()) after the call on every path:--raw, the success wrapper and a failed call. This keeps it consistent withtg auth, which closes its client before it exits. A failure to close is ignored, so the result still stands. On the paths that already exited, this has no effect users can see:process.exit()already saves queued session writes through mtcute's exit hook.I went slightly beyond the issue's suggested fix, which only covered
--raw, and close the client on all three paths, as the plan asked ("every path of tg api").Tests
New
src/__e2e__/api.e2e.test.tsruns the real CLI entry throughsrc/__e2e__/helpers/fake-client-cli.ts(via a newentryoption onrunCli). That helper replacesgetClientForAccountwith a fake client that keeps the process alive even afterdestroy(), as a connected mtcute client might, so the command ends only through an explicit exit. Itsdestroy()waits before it prints a marker, as the real one awaits the connection and session shutdown, so a caller that exits without awaiting it fails the test. With a seeded account and no network:tg api help.getConfig --rawprints the bare result and exits 0 (before the fix it hung until the 8 s timeout), and the client is closedtg api help.getConfigprints the success wrapper and exits 0, and the client is closedTELEGRAM_ERROR, and the client is closedAll three failed on
main(the last two only because the client was never closed, which users could not see) and pass with the fix. Changing eitherawait closeClient(client)tovoid closeClient(client)fails all three, and dropping the explicit exit fromraw()makes the--rawtest time out. I also ran an offline check with a real mtcute client in replay mode (TELEGRAM_API_REPLAY=1with a made-up fixture):--rawprinted the result and exited 0 in under 100 ms. No live Telegram checks.How I checked it
bun run check: cleanbun run test: 1709 passbun run test:e2e: 255 passTests pass locally and use made-up data only
README, docs and affected demos follow the product content standard (
docs/api-design.mdsays how--rawexits, and its implementation sample now closes the client and prints throughraw();docs/testing.mdlists the new e2e file)Title follows the repo's commit style
🤖 Generated with Claude Code