Skip to content

Fix streaming cancellation and webhook concurrency (Issue #79)#80

Open
Dhereal1 wants to merge 3 commits intoHyperlinksSpace:mainfrom
Dhereal1:fix/thread-cancellation
Open

Fix streaming cancellation and webhook concurrency (Issue #79)#80
Dhereal1 wants to merge 3 commits intoHyperlinksSpace:mainfrom
Dhereal1:fix/thread-cancellation

Conversation

@Dhereal1
Copy link
Copy Markdown
Contributor

Summary

Fixes streaming cancellation and concurrency issues in the Telegram bot.

Changes

  • Corrected abort logic to only cancel when newer updates arrive (max > current update_id)
  • Ensured streaming stops immediately using AbortController
  • Added generation-based guards to prevent stale updates
  • Made streaming non-blocking (no awaited Telegram edits)
  • Improved logging by including update_id for better tracing

Result

  • Fixes issue where responses stopped after %
  • Enables proper interrupt + restart behavior
  • Ensures multiple messages can be handled concurrently without race conditions

Testing

  • Tested locally using webhook + ngrok
  • Verified:
    • cancel stops stream immediately
    • new messages start new generations
    • no stale edits after cancel
    • multi-thread handling works

Log Evidence

[START] new generation bot:... 7 update: 802576331
[bot][cancel] aborted previous generation
[CANCEL] aborting stream
[STREAM] aborted immediately

…ntroller

- Introduce thread-scoped AbortController map

- Abort previous generation when new message arrives for same thread

- Propagate abort signal to AI streaming layer

- Stop Telegram edit loop immediately on abort

- Preserve existing HTML + chunking behavior

Implements G1 (hard cancellation). Multi-thread concurrency intentionally deferred.
- fix incorrect abort logic (max > current update_id)

- ensure streams stop immediately on cancel

- prevent stale generations from sending updates

- add generation-based cancellation guard

- make streaming non-blocking (no awaited edits)

- improve logging with update_id for tracing
Copy link
Copy Markdown
Member

@staindart staindart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not working on test, message cancellation does not actually function, please double check and implenent with program version stipulated on start message.

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.

2 participants