lnutils: add ContextFromQuit to bridge quit channels to contexts
What changed, and why it matters
This commit adds a small internal helper function that lets existing shutdown channels in lnd work with newer code that expects Go's standard context-based cancellation. It does not change any behavior, fix any bug, or introduce any user-facing feature. There is no security relevance in the change itself.
No security action needed. Treat as routine code hygiene/infrastructure. Future callers should ensure the returned cancel function is deferred to prevent goroutine leaks, as documented.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces ContextFromQuit in lnutils/context.go. It takes a <-chan struct{} quit channel, creates a cancellable context derived from context.Background(), and starts a goroutine that cancels the context when either the quit channel closes or the context is already cancelled. The caller must invoke the returned CancelFunc to avoid leaking the goroutine. This is a pure utility with no policy, no timeout, and no callers yet added in this commit.
Changed components
lnutils/context.goInspect captured patch +22 / −0
diff --git a/lnutils/context.go b/lnutils/context.go
new file mode 100644
index 0000000..5deeb77
--- /dev/null
+++ b/lnutils/context.go
@@ -0,0 +1,22 @@
+package lnutils
+
+import "context"
+
+// ContextFromQuit returns a context that is cancelled when the provided quit
+// channel is closed. The returned cancel function MUST be called to avoid
+// goroutine leaks.
+func ContextFromQuit(quit <-chan struct{}) (context.Context,
+ context.CancelFunc) {
+
+ ctx, cancel := context.WithCancel(context.Background())
+
+ go func() {
+ select {
+ case <-quit:
+ cancel()
+ case <-ctx.Done():
+ }
+ }()
+
+ return ctx, cancel
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.