Skip to content

lightningd: parent watchman's formatted blockhash strings to tmpctx - #9363

Open
ksedgwic wants to merge 1 commit into
ElementsProject:masterfrom
ksedgwic:fix-watchman-memleak-report
Open

lightningd: parent watchman's formatted blockhash strings to tmpctx#9363
ksedgwic wants to merge 1 commit into
ElementsProject:masterfrom
ksedgwic:fix-watchman-memleak-report

Conversation

@ksedgwic

Copy link
Copy Markdown
Collaborator

Fixes #9362 : the memleak scanner can flag the blockhash strings
watchman formats into its RPC responses -- json_add_string copies the
value, so nothing references the string afterward, and a dev memleak
check racing an in-flight response reports it as a leak. Parent them
to tmpctx, the idiom used elsewhere.

Changelog-None

json_add_string copies its value, so the strings fmt_bitcoin_blkid
allocates in json_block_processed and json_getwatchmanheight are
referenced by nothing once the call returns.  They are parented to
the response stream and freed with it, but the memleak scanner works
by searching memory for pointers to each allocation: when a dev
memleak check races an in-flight response, the unreferenced string is
reported as a leak and fails the test run (seen in a liquid CI run of
test_bwatch_add_watch_creates_datastore_entry).  Parent them to
tmpctx, the idiom used elsewhere.

Fixes: ElementsProject#9362
Changelog-None
@ksedgwic
ksedgwic requested a review from sangbida July 28, 2026 19:36

@Andezion Andezion left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you know why parenting to response specifically trips the memleak scanner here, given response is itself reachable through the command's tal tree? Is it because the scan can race a not yet completed response object mid-write?

@ksedgwic

ksedgwic commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

json_add_string copies the bytes into the stream buffer, so nothing holds a pointer to the fmt_bitcoin_blkid string once the call returns; its only tie is the tal parent link. So yes -- when a dev-memleak check lands while a response is still in flight, the string is still allocated, referenced by nothing, and gets reported. Parenting to tmpctx closes that window.

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.

CI: memleak reports for watchman's response-parented blockhash strings

3 participants