Skip to content

fix(api): close the client on every path so tg api --raw exits - #71

Merged
neoromantic merged 4 commits into
mainfrom
fix/api-raw-exit-69
Oct 22, 2026
Merged

neoromantic merged 4 commits into
mainfrom
fix/api-raw-exit-69

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

What and why

tg api <method> --raw printed the result and then hung until it was killed. The --raw path printed with console.log and returned. Only success() and error() exit the process, so mtcute's open connection kept the event loop alive. Without --raw and on a failed call, success() and error() already exited the process, so those paths finished as expected.

Fixes #69

Fix

  • --raw now prints through a new raw() helper in src/utils/output.ts, which exits the way success() 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 calls process.exit after it closes its clients), and this can't be checked offline against a connected mtcute client. So --raw exits explicitly, like every other output path.
  • tg api also closes the client (client.destroy()) after the call on every path: --raw, the success wrapper and a failed call. This keeps it consistent with tg 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.ts runs the real CLI entry through src/__e2e__/helpers/fake-client-cli.ts (via a new entry option on runCli). That helper replaces getClientForAccount with a fake client that keeps the process alive even after destroy(), as a connected mtcute client might, so the command ends only through an explicit exit. Its destroy() 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 --raw prints the bare result and exits 0 (before the fix it hung until the 8 s timeout), and the client is closed
  • tg api help.getConfig prints the success wrapper and exits 0, and the client is closed
  • a failed call exits 5 with TELEGRAM_ERROR, and the client is closed

All 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 either await closeClient(client) to void closeClient(client) fails all three, and dropping the explicit exit from raw() makes the --raw test time out. I also ran an offline check with a real mtcute client in replay mode (TELEGRAM_API_REPLAY=1 with a made-up fixture): --raw printed the result and exited 0 in under 100 ms. No live Telegram checks.

How I checked it

  • bun run check: clean

  • bun run test: 1709 pass

  • bun run test:e2e: 255 pass

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/api-design.md says how --raw exits, and its implementation sample now closes the client and prints through raw(); docs/testing.md lists the new e2e file)

  • Title follows the repo's commit style

🤖 Generated with Claude Code

neoromantic and others added 4 commits October 22, 2026 15:49
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]>
@neoromantic
neoromantic merged commit 652b180 into main Oct 22, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/api-raw-exit-69 branch October 22, 2026 12:42
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.

tg api --raw prints the result and never exits

1 participant