Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Electrum RPC Hang #48

Merged
merged 1 commit into from
Sep 25, 2023
Merged

Conversation

junderw
Copy link
Member

@junderw junderw commented Sep 22, 2023

Edit: This PR by itself has a bug that was fixed in #49


This works now.

SIGINT shuts everything down within a few milliseconds. (I only had one active connection from my local Sparrow wallet though)

You can test it by:

  1. Build and run it, be sure to pass in --electrum-rpc-addr "0.0.0.0:50001" etc to allow your electrs to listen outside the container if using docker etc. (also expose the ports)
  2. Wait until it says Electrum RPC started.
  3. Run commands below to pummel the server with electrum requests. Wait for maybe 10 seconds or so before sending SIGINT.
  4. Send a SIGINT. Pre-merge, it would freeze and not shutdown. Post-merge, the process will shut down in a few milliseconds.

Commands

# Make a temp folder and cd to it
cd $(mktemp -d)

# create a new rust project
cargo new electrs-pummeler

cd electrs-pummeler

cargo add [email protected]

##### Enter the Rust code below into main.rs

# Only run the code after the Electrum RPC is listening on 50001 localhost
cargo run --release
use electrum_client::{bitcoin::Script, Client, ElectrumApi};
use std::thread::{sleep, spawn};
use std::time::Duration;

const LONG_WAIT_MS: u64 = 600_000;
const TINY_WAIT_MS: u64 = 10;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    for _ in 0..5000 {
        spawn(move || {
            let _client = run_new_client_in_loop("tcp://127.0.0.1:50001").unwrap();

            sleep(Duration::from_millis(LONG_WAIT_MS));
        });
        sleep(Duration::from_millis(TINY_WAIT_MS));
    }
    sleep(Duration::from_millis(LONG_WAIT_MS));
    Ok(())
}

fn run_new_client_in_loop(url: &str) -> Result<Client, Box<dyn std::error::Error>> {
    let client = Client::new(url)?;

    let _ = client.block_headers_subscribe()?;
    let mut script_bytes = [0x04, 0, 0, 0, 0];
    let mut subs = Vec::with_capacity(256);
    for i in 0..=255u8 {
        script_bytes[4] = i;
        subs.push(client.script_subscribe(Script::from_bytes(&script_bytes))?);
    }
    Ok(client)
}

@junderw junderw marked this pull request as draft September 22, 2023 02:56
@junderw junderw force-pushed the junderw/electrum-rpc-graceful-exit branch 4 times, most recently from 627dc8c to da9099f Compare September 23, 2023 02:44
@junderw junderw marked this pull request as ready for review September 23, 2023 02:47
@junderw junderw changed the title WIP: Fix Electrum RPC Hang Fix Electrum RPC Hang Sep 23, 2023
@junderw junderw marked this pull request as draft September 23, 2023 07:11
@junderw junderw force-pushed the junderw/electrum-rpc-graceful-exit branch from da9099f to 70041f2 Compare September 23, 2023 08:21
@junderw junderw marked this pull request as ready for review September 23, 2023 08:21
@junderw junderw force-pushed the junderw/electrum-rpc-graceful-exit branch 4 times, most recently from 519def5 to 98f2e0a Compare September 24, 2023 21:09
@junderw junderw force-pushed the junderw/electrum-rpc-graceful-exit branch from 98f2e0a to 0af9038 Compare September 24, 2023 22:54
Copy link
Contributor

@mononaut mononaut left a comment

Choose a reason for hiding this comment

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

Tested ACK @ 0af9038

I was able to reproduce the hang ~50% of the time on master using the pummeler, but not at all running this PR.

I don't fully understand all of the code changes, but it seems to run fine and doesn't break the RPC API as far as I can tell (if anything, RPC commands/subscriptions feel faster).

Copy link
Member

@softsimon softsimon left a comment

Choose a reason for hiding this comment

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

tested ACK @ [0af9038]

@softsimon softsimon merged commit 4ed52c3 into mempool Sep 25, 2023
@softsimon softsimon deleted the junderw/electrum-rpc-graceful-exit branch September 25, 2023 15:59
SatoKentaNayoro pushed a commit to boolnetwork/mempool-electrs that referenced this pull request Nov 29, 2024
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