Skip to content

xpay: add a flag to indicate completion of initialization#9250

Open
Lagrang3 wants to merge 1 commit into
ElementsProject:masterfrom
Lagrang3:xpay-startup
Open

xpay: add a flag to indicate completion of initialization#9250
Lagrang3 wants to merge 1 commit into
ElementsProject:masterfrom
Lagrang3:xpay-startup

Conversation

@Lagrang3

@Lagrang3 Lagrang3 commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Add a "ready" flag that becomes true after the plugin has finished initializing. Usually RPC calls during initialization are done using synchronous communication but xpay uses hooks to bind itself to "pay". For some reason registering to hooks and using rcp_scan at init cannot be done. Therefore xpay's initialization is asynchronous which has the downside of race conditions like: trying to make a payment while xpay does not know yet the current node id. This is unlikely to happen in real life but it breaks our tests randomly.

Fixes flake tests/test_xpay.py:test_xpay_fake_channeld

Valgrind error file: valgrind-errors.356233
==356233== Conditional jump or move depends on uninitialised value(s)
==356233==    at 0x1116ED: handle_block_added (xpay.c:3159)
==356233==    by 0x11D7A8: ld_command_handle (libplugin.c:2144)
==356233==    by 0x11DB8A: ld_read_json (libplugin.c:2282)
==356233==    by 0x15DAC1: next_plan (io.c:60)
==356233==    by 0x15DF4C: do_plan (io.c:422)
==356233==    by 0x15E005: io_ready (io.c:439)
==356233==    by 0x15F99B: io_loop (poll.c:470)
==356233==    by 0x11DFD6: plugin_main (libplugin.c:2481)
==356233==    by 0x11876E: main (xpay.c:3412)
==356233==
{
   <insert_a_suppression_name_here>
   Memcheck:Cond
   fun:handle_block_added
   fun:ld_command_handle
   fun:ld_read_json
   fun:next_plan
   fun:do_plan
   fun:io_ready
   fun:io_loop
   fun:plugin_main
   fun:main
}

Fixes #9248

@Lagrang3 Lagrang3 requested review from cdecker and ddustin June 24, 2026 10:46
@Lagrang3

Copy link
Copy Markdown
Collaborator Author

I am not sure we should do this. We have never done this pattern before AFAIK.
Another workaround could be to wait until askrene has the xpay layer, or maybe do a lazy initialization (ie. don't initialize at init but instead initialize if we receive a request and we haven't initialized yet).
Or we could fix lightningd so that plugins that use command hooks can also use rpc_scan.

Add a "ready" flag that becomes true after the plugin has finished
initializing. Usually RPC calls during initialization are done using
synchronous communication but xpay uses hooks to bind itself to "pay".
For some reason registering to hooks and using rcp_scan at init cannot
be done. Therefore xpay's initialization is asynchronous which has the
downside of race conditions like: trying to make a payment while xpay
does not know yet the current node id. This is unlikely to happen in
real life but it breaks our tests randomly.

Fixes flake `tests/test_xpay.py:test_xpay_fake_channeld`
```
Valgrind error file: valgrind-errors.356233
==356233== Conditional jump or move depends on uninitialised value(s)
==356233==    at 0x1116ED: handle_block_added (xpay.c:3159)
==356233==    by 0x11D7A8: ld_command_handle (libplugin.c:2144)
==356233==    by 0x11DB8A: ld_read_json (libplugin.c:2282)
==356233==    by 0x15DAC1: next_plan (io.c:60)
==356233==    by 0x15DF4C: do_plan (io.c:422)
==356233==    by 0x15E005: io_ready (io.c:439)
==356233==    by 0x15F99B: io_loop (poll.c:470)
==356233==    by 0x11DFD6: plugin_main (libplugin.c:2481)
==356233==    by 0x11876E: main (xpay.c:3412)
==356233==
{
   <insert_a_suppression_name_here>
   Memcheck:Cond
   fun:handle_block_added
   fun:ld_command_handle
   fun:ld_read_json
   fun:next_plan
   fun:do_plan
   fun:io_ready
   fun:io_loop
   fun:plugin_main
   fun:main
}

```

Changelog-None

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
@Lagrang3

Copy link
Copy Markdown
Collaborator Author

Another way to solve this is to remove pay's backend and implement a json_pay into xpay.c that simply uses xpay
so that we can remove the hook. pay is in any case deprecated and we keep our promise to support the API.

@Lagrang3 Lagrang3 mentioned this pull request Jun 24, 2026
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.

Valgrind error on test_xpay_fake_channeld

1 participant