Repository: nervosnetwork/ckb (language: rust)

Issue:
`ckb miner` doesn't exit when `--limit` flag is specified
## Bug Report

<!-- Thanks for your bug report! We appreciate your time to fill following sections to help us locate the root cause. -->

### Current Behavior
Start miner by `ckb miner --limit 2`, it doesn't exit after 2 nonces found.
```
$ ckb miner --limit 2
2023-07-06 01:26:52.923 +00:00 main INFO sentry  sentry is disabled
2023-07-06 01:26:52.923 +00:00 main INFO ckb_bin::helper  raise_fd_limit newly-increased limit: 524288
2023-07-06 01:26:52.941 +00:00 main INFO ckb_memory_tracker::process  track current process: disable
2023-07-06 01:26:52.941 +00:00 main INFO ckb_miner::client  loop poll mode: interval 1000ms
Found! #7 0x33c8d9b4861b0bc57edb82fd0b10edd65623ee65acc95c941319e7e8bcd4d3ff
Found! #8 0x9b81f48ebfdf170f1f5e38280f637b926c6f00f5ace7b2d49117818441b68687
Dummy-Worker ⠁ [00:00:00]
2023-07-06 01:27:08.943 +00:00 GlobalRt-3 ERROR ckb_miner::client  notify_new_block error: "SendError(..)"
2023-07-06 01:27:12.944 +00:00 Dummy-Worker ERROR ckb_miner::worker::dummy  nonce_tx send error "SendError(..)"
2023-07-06 01:27:17.944 +00:00 Dummy-Worker ERROR ckb_miner::worker::dummy  nonce_tx send error "SendError(..)"
2023-07-06 01:27:22.944 +00:00 Dummy-Worker ERROR ckb_miner::worker::dummy  nonce_tx send error "SendError(..)"

```

### Expected Behavior
Process exit after specified limit nonces found.
Ref: #1992
### Environment

- **CKB version**: `v0.110.0`
- **Chain**: `dev`
- **Operating system**: `Linux 6.4.0, NixOS, 23.11 (Tapir), 23.11.20230704.78419ed`
- **Arch**: `Linux Mufasa 6.4.0 #1-NixOS SMP PREEMPT_DYNAMIC Sun Jun 25 23:29:58 UTC 2023 x86_64 GNU/Linux`
- **Installation**: GitHub Release

### Additional context/Screenshots
<!-- Add any other context about the problem here. If applicable, add ckb.toml and screenshots to help explain. -->



Discussion / hints:

Fix patch:
diff --git a/miner/src/miner.rs b/miner/src/miner.rs
index 110fad5514..101f156b4d 100644
--- a/miner/src/miner.rs
+++ b/miner/src/miner.rs
@@ -5,6 +5,7 @@ use ckb_app_config::MinerWorkerConfig;
 use ckb_channel::{select, unbounded, Receiver};
 use ckb_logger::{debug, error, info};
 use ckb_pow::PowEngine;
+use ckb_stop_handler::broadcast_exit_signals;
 use ckb_types::{
     packed::{Byte32, Header},
     prelude::*,
@@ -94,7 +95,8 @@ impl Miner {
                     Ok((pow_hash, work, nonce)) => {
                         self.submit_nonce(pow_hash, work, nonce);
                         if self.limit != 0 && self.nonces_found >= self.limit {
-                            break;
+                            debug!("miner nonce limit reached, terminate ...");
+                            broadcast_exit_signals();
                         }
                     },
                     _ => {
diff --git a/util/stop-handler/src/stop_register.rs b/util/stop-handler/src/stop_register.rs
index 8948217c19..d7f2a7f560 100644
--- a/util/stop-handler/src/stop_register.rs
+++ b/util/stop-handler/src/stop_register.rs
@@ -12,8 +12,8 @@ pub fn wait_all_ckb_services_exit() {
     info!("waiting exit signal...");
     let exit_signal = new_crossbeam_exit_rx();
     let _ = exit_signal.recv();
-    debug!("received exit signal, broadcasting exit signal to all threads");
     let mut handles = CKB_HANDLES.lock();
+    debug!("wait_all_ckb_services_exit wait all threads to exit");
     for (name, join_handle) in handles.thread_handles.drain(..) {
         match join_handle.join() {
             Ok(_) => {
@@ -54,6 +54,7 @@ pub fn new_crossbeam_exit_rx() -> ckb_channel::Receiver<()> {
 
 /// Broadcast exit signals to all threads and all tokio tasks
 pub fn broadcast_exit_signals() {
+    debug!("received exit signal, broadcasting exit signal to all threads");
     TOKIO_EXIT.cancel();
     CROSSBEAM_EXIT_SENDERS
         .lock()


Test patch: