Skip to content

submarine_swaps: wait for broadcast in wait_for_htlcs_and_broadcast#10661

Open
f321x wants to merge 1 commit into
spesmilo:masterfrom
f321x:fix_race_swap_regtest
Open

submarine_swaps: wait for broadcast in wait_for_htlcs_and_broadcast#10661
f321x wants to merge 1 commit into
spesmilo:masterfrom
f321x:fix_race_swap_regtest

Conversation

@f321x
Copy link
Copy Markdown
Member

@f321x f321x commented May 20, 2026

Wait for broadcast of the funding transaction in SwapManager.wait_for_htlcs_and_broadcast before returning. Previously it might have returned the swap funding txid before the funding tx was broadcast.
This caused the regtest test_swapserver_success_forward to fail in the CI as it expected the funding tx to be broadcast once the CLI command returned.

See:

.***** test_swapserver_success_forward ******
initializing alice
funding alice
ec354953f96e0de7be4354ab156611da9e31bd85c2d25664bdb995950530c12e
initializing bob
funding bob
1df19a419b5ae5b961bae6cfaf4e7c8e772c6632d30782fab8de6992a8901e4a
mining 1 blocks
starting daemon (PID 7471)
/tmp/alice/regtest/wallets/default_wallet
true
starting daemon (PID 7502)
/tmp/bob/regtest/wallets/default_wallet
true

alice opens channel
mining 3 blocks
wait until alice sees channel open.
wait until alice sees channel open..
wait until alice sees channel open...
alice initiates forward-swap
{
  "lightning_amount": "0.01967661",
  "onchain_amount": "0.02",
  "txid": "b0f9ba48b670ee4a1f182655fe18f3f7d5c310923b7c85ad396b24e0a2f8e0cc"
}
utxo b0f9ba48b670ee4a1f182655fe18f3f7d5c310923b7c85ad396b24e0a2f8e0cc:0 does not exist
FDaemon stopped
Daemon stopped

======================================================================
FAIL: test_swapserver_success_forward (tests.regtest.TestLightningSwapserver.test_swapserver_success_forward)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/cirrus-ci-build/tests/regtest.py", line 105, in test_swapserver_success_forward
    self.run_shell(['swapserver_success_forward'])
  File "/tmp/cirrus-ci-build/tests/regtest.py", line 19, in run_shell
    assert process.returncode == 0
           ^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Can be reproduced with:

diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py
index f83d653f9..242aef070 100644
--- a/electrum/submarine_swaps.py
+++ b/electrum/submarine_swaps.py
@@ -1052,6 +1052,7 @@ class SwapManager(Logger):
     @log_exceptions
     async def broadcast_funding_tx(self, swap: SwapData, tx: Transaction) -> None:
         swap.funding_txid = tx.txid()
+        await asyncio.sleep(5)
         await self.network.broadcast_transaction(tx)

     async def reverse_swap(

@f321x f321x added the bug 🐞 label May 20, 2026
@f321x f321x force-pushed the fix_race_swap_regtest branch 3 times, most recently from 840a7aa to 7dbaaa9 Compare May 20, 2026 16:09
Wait for broadcast of the funding transaction in
`SwapManager.wait_for_htlcs_and_broadcast` before returning.
Previously it might have returned the swap funding txid before the
funding tx was broadcast.
This caused the regtest `test_swapserver_success_forward` to fail
as it expected the funding tx to be broadcast once the CLI command
returned.

See:
```
.***** test_swapserver_success_forward ******
initializing alice
funding alice
ec354953f96e0de7be4354ab156611da9e31bd85c2d25664bdb995950530c12e
initializing bob
funding bob
1df19a419b5ae5b961bae6cfaf4e7c8e772c6632d30782fab8de6992a8901e4a
mining 1 blocks
starting daemon (PID 7471)
/tmp/alice/regtest/wallets/default_wallet
true
starting daemon (PID 7502)
/tmp/bob/regtest/wallets/default_wallet
true

alice opens channel
mining 3 blocks
wait until alice sees channel open.
wait until alice sees channel open..
wait until alice sees channel open...
alice initiates forward-swap
{
  "lightning_amount": "0.01967661",
  "onchain_amount": "0.02",
  "txid": "b0f9ba48b670ee4a1f182655fe18f3f7d5c310923b7c85ad396b24e0a2f8e0cc"
}
utxo b0f9ba48b670ee4a1f182655fe18f3f7d5c310923b7c85ad396b24e0a2f8e0cc:0 does not exist
FDaemon stopped
Daemon stopped

======================================================================
FAIL: test_swapserver_success_forward (tests.regtest.TestLightningSwapserver.test_swapserver_success_forward)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/cirrus-ci-build/tests/regtest.py", line 105, in test_swapserver_success_forward
    self.run_shell(['swapserver_success_forward'])
  File "/tmp/cirrus-ci-build/tests/regtest.py", line 19, in run_shell
    assert process.returncode == 0
           ^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
```

Can be reproduced with:
```diff
diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py
index f83d653..242aef070 100644
--- a/electrum/submarine_swaps.py
+++ b/electrum/submarine_swaps.py
@@ -1052,6 +1052,7 @@ class SwapManager(Logger):
     @log_exceptions
     async def broadcast_funding_tx(self, swap: SwapData, tx: Transaction) -> None:
         swap.funding_txid = tx.txid()
+        await asyncio.sleep(5)
         await self.network.broadcast_transaction(tx)

     async def reverse_swap(
```
@f321x f321x force-pushed the fix_race_swap_regtest branch from 7dbaaa9 to 2a1eda4 Compare May 20, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant