Skip to content

fix(tasks): Refactor send_zap to use async websockets and prevent crashes#90

Merged
dni merged 2 commits intolnbits:mainfrom
santyr:main
Aug 13, 2025
Merged

fix(tasks): Refactor send_zap to use async websockets and prevent crashes#90
dni merged 2 commits intolnbits:mainfrom
santyr:main

Conversation

@santyr
Copy link
Contributor

@santyr santyr commented Aug 8, 2025

Problem:

The send_zap function was causing application-wide freezes and intermittent crashes. This was due to using a synchronous, thread-based websocket library (websocket-client) from within an asyncio coroutine.

Solution:

This pull request refactors the send_zap function to be fully asynchronous and safe by replacing the threaded implementation with the native asyncio websockets library (which was already a project dependency).

Removes the threading and websocket-client libraries from tasks.py.

Uses asyncio.create_task to send zap receipts to relays concurrently on a "fire-and-forget" basis.

Adds proper timeout and error handling for each connection.

This change eliminates the root cause of the instability, making the send_zap operation non-blocking, efficient, and safe from race conditions.

santyr added 2 commits August 8, 2025 08:06
The send_zap async function contained blocking calls (thread.join()) which halted the main asyncio event loop. This caused the application to become unresponsive or "freeze" until all zap receipt threads completed.

Refactored the function to be fully non-blocking by removing the join() calls and the arbitrary sleep(). Zap receipts are now dispatched in background threads on a fire-and-forget basis, allowing the main application to remain responsive.
Refactor to replace the threading and websocket-client logic with the native asyncio websockets library. 

Create a non-blocking asyncio task for each relay,
Copy link
Member

@dni dni left a comment

Choose a reason for hiding this comment

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

love it, thanks!

@dni dni merged commit 7eea233 into lnbits:main Aug 13, 2025
@santyr
Copy link
Contributor Author

santyr commented Aug 14, 2025

You're welcome. Really, I'm just submitting these PRs as part of fixing my goat feeder. https://lightning-goats.com ;)

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.

3 participants