Skip to content

cln-plugin: fix missed-wakeup deadlock in Plugin::join and Plugin::shutdown - #9757

Merged
cdecker merged 1 commit into
ElementsProject:masterfrom
daywalker90:cln-plugin-fix-join-shutdown
Dec 2, 2026
Merged

cdecker merged 1 commit into
ElementsProject:masterfrom
daywalker90:cln-plugin-fix-join-shutdown

Conversation

@daywalker90

Copy link
Copy Markdown
Collaborator

Plugin::wait_handle used a tokio::sync::broadcast::Sender<()> with
its initial receiver immediately dropped. If PluginDriver::run exited
or Plugin::shutdown() was called before the caller reached
Plugin::join(), broadcast::Sender::send(()) failed with zero
receivers and dropped the signal; subsequent Plugin::join() calls then
hung indefinitely because Plugin itself still held the Sender. In
addition, PluginDriver::run never observed wait_handle, so
Plugin::shutdown() did not stop the driver loop.

Replace the broadcast channel with a tokio::sync::watch::Sender<bool>
(updated via send_replace(true)), drop a guard on driver exit so the
state is always flipped even on unwind, and select on the watch channel
in PluginDriver::run so shutdown() stops the driver loop cleanly.

Add unit tests covering join() after driver exit, shutdown() before
join(), and shutdown() waking a waiting join().

Changelog-Fixed: cln-plugin: fix missed-wakeup deadlock in Plugin::join() and make Plugin::shutdown() reliably stop the driver loop.

@daywalker90
daywalker90 requested a review from cdecker as a code owner November 24, 2026 12:01
@daywalker90
daywalker90 force-pushed the cln-plugin-fix-join-shutdown branch from 9d887d3 to 8d4d002 Compare November 25, 2026 14:23
@cdecker
cdecker enabled auto-merge (rebase) November 27, 2026 12:27
cdecker
cdecker previously approved these changes Nov 27, 2026
@cdecker
cdecker force-pushed the cln-plugin-fix-join-shutdown branch from 8d4d002 to 458845d Compare November 27, 2026 12:34
Comment thread plugins/src/lib.rs Resolved
…:shutdown`

`Plugin::wait_handle` used a `tokio::sync::broadcast::Sender<()>` with
its initial receiver immediately dropped. If `PluginDriver::run` exited
or `Plugin::shutdown()` was called before the caller reached
`Plugin::join()`, `broadcast::Sender::send(())` failed with zero
receivers and dropped the signal; subsequent `Plugin::join()` calls then
hung indefinitely because `Plugin` itself still held the `Sender`. In
addition, `PluginDriver::run` never observed `wait_handle`, so
`Plugin::shutdown()` did not stop the driver loop.

Replace the broadcast channel with a `tokio::sync::watch::Sender<ShutdownState>`
tracking `Running`, `ShuttingDown`, and `Stopped`:

- `Plugin::shutdown()` transitions `Running` to `ShuttingDown`.
- `PluginDriver::run` observes `ShuttingDown`, stops reading `input`,
  closes `receiver`, and drains any already-queued outgoing messages to
  `output` before exiting.
- `ShutdownGuard` transitions the state to `Stopped` on driver exit
  (including unwinds), so `Plugin::join()` wakes only after queued
  outgoing messages have been delivered.

Add unit tests covering `join()` after driver exit, `shutdown()` before
`join()`, `shutdown()` waking a waiting `join()`, and draining queued
outgoing messages before `join()` returns.

Changelog-Fixed: cln-plugin: fix missed-wakeup deadlock in `Plugin::join()` and make `Plugin::shutdown()` cleanly stop the driver loop after draining queued outgoing messages.
@daywalker90
daywalker90 force-pushed the cln-plugin-fix-join-shutdown branch from 458845d to 3a22ba5 Compare December 2, 2026 11:30
@daywalker90
daywalker90 requested a review from jaonoctus December 2, 2026 11:46
@cdecker
cdecker merged commit 7fae0ec into ElementsProject:master Dec 2, 2026
56 checks passed
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