@@ -3,8 +3,10 @@ use std::io::Read;
33use color_eyre:: eyre:: Result ;
44use namada_sdk:: io:: { Io , NamadaIo , display_line} ;
55use namada_sdk:: masp:: ShieldedContext ;
6- use namada_sdk:: { Namada , NamadaImpl } ;
6+ use namada_sdk:: masp:: fs:: FsShieldedUtils ;
7+ use namada_sdk:: { Namada , NamadaImpl , ShieldedWallet } ;
78use namada_wallet:: DatedViewingKey ;
9+ use tokio:: signal:: unix:: SignalKind ;
810
911use crate :: cli;
1012use crate :: cli:: api:: { CliApi , CliClient } ;
@@ -97,8 +99,8 @@ impl CliApi {
9799 )
98100 . expect ( "Missing required shielded-sync arguments" ) ;
99101 sync_args. viewing_keys . extend ( extra_sync_vks) ;
100- let shielded = crate :: client :: masp :: syncing (
101- ShieldedContext :: new ( shielded) ,
102+ let shielded = Self :: scoped_shielded_sync (
103+ shielded,
102104 client. clone ( ) ,
103105 sync_args,
104106 & io,
@@ -155,8 +157,8 @@ impl CliApi {
155157 )
156158 . expect ( "Missing required shielded-sync arguments" ) ;
157159 sync_args. viewing_keys . extend ( extra_sync_vks) ;
158- let shielded = crate :: client :: masp :: syncing (
159- ShieldedContext :: new ( shielded) ,
160+ let shielded = Self :: scoped_shielded_sync (
161+ shielded,
160162 client. clone ( ) ,
161163 sync_args,
162164 & io,
@@ -203,8 +205,8 @@ impl CliApi {
203205 . expect ( "Missing required shielded-sync arguments" ) ;
204206 sync_args. viewing_keys . extend ( extra_sync_vks) ;
205207 let synced_shielded_ctx =
206- crate :: client :: masp :: syncing (
207- ShieldedContext :: new ( shielded) ,
208+ Self :: scoped_shielded_sync (
209+ shielded,
208210 client. clone ( ) ,
209211 sync_args,
210212 & io,
@@ -298,8 +300,8 @@ impl CliApi {
298300 . expect ( "Missing required shielded-sync arguments" ) ;
299301 sync_args. viewing_keys . extend ( extra_sync_vks) ;
300302 let synced_shielded_ctx =
301- crate :: client :: masp :: syncing (
302- ShieldedContext :: new ( shielded) ,
303+ Self :: scoped_shielded_sync (
304+ shielded,
303305 client. clone ( ) ,
304306 sync_args,
305307 & io,
@@ -786,8 +788,8 @@ impl CliApi {
786788 . expect ( "Missing required shielded-sync arguments" ) ;
787789 sync_args. viewing_keys . extend ( extra_sync_vks) ;
788790 let synced_shielded_ctx =
789- crate :: client :: masp :: syncing (
790- ShieldedContext :: new ( shielded) ,
791+ Self :: scoped_shielded_sync (
792+ shielded,
791793 client. clone ( ) ,
792794 sync_args,
793795 & io,
@@ -839,8 +841,8 @@ impl CliApi {
839841 )
840842 . expect ( "Missing required shielded-sync arguments" ) ;
841843 sync_args. viewing_keys . extend ( extra_sync_vks) ;
842- let shielded = crate :: client :: masp :: syncing (
843- ShieldedContext :: new ( shielded) ,
844+ let shielded = Self :: scoped_shielded_sync (
845+ shielded,
844846 client. clone ( ) ,
845847 sync_args,
846848 & io,
@@ -1197,4 +1199,55 @@ impl CliApi {
11971199 }
11981200 Ok ( ( ) )
11991201 }
1202+
1203+ // A workaround for the custom signal handlers installed by the
1204+ // shielded-sync process. To prevent a hanging rpc from stalling the cli
1205+ // command we need to uninstall the custom handlers. Unfortunately these
1206+ // can't be removed, not even by dropping the
1207+ // [`tokio::signal::unix::Signal`] type. This function tries to restore the
1208+ // default behavior by simply listening for the events on the stream and
1209+ // calling [`panic`].
1210+ async fn scoped_shielded_sync < IO > (
1211+ shielded : ShieldedWallet < FsShieldedUtils > ,
1212+ client : impl CliClient ,
1213+ sync_args : namada_sdk:: args:: ShieldedSync ,
1214+ io : & IO ,
1215+ ) -> Result < ShieldedContext < FsShieldedUtils > >
1216+ where
1217+ IO : Io + Send + Sync ,
1218+ {
1219+ let shielded = crate :: client:: masp:: syncing (
1220+ ShieldedContext :: new ( shielded) ,
1221+ client,
1222+ sync_args,
1223+ io,
1224+ )
1225+ . await ?;
1226+
1227+ tokio:: spawn ( async {
1228+ let mut sigterm =
1229+ tokio:: signal:: unix:: signal ( SignalKind :: terminate ( ) ) . unwrap ( ) ;
1230+ let mut sighup =
1231+ tokio:: signal:: unix:: signal ( SignalKind :: hangup ( ) ) . unwrap ( ) ;
1232+ let mut sigpipe =
1233+ tokio:: signal:: unix:: signal ( SignalKind :: pipe ( ) ) . unwrap ( ) ;
1234+
1235+ tokio:: select! {
1236+ _ = tokio:: signal:: ctrl_c( ) => {
1237+ panic!( "Received interrupt signal, exiting..." ) ;
1238+ } ,
1239+ _= sigterm. recv( ) => {
1240+ panic!( "Received termination signal, exiting..." ) ;
1241+ } ,
1242+ _ = sighup. recv( ) => {
1243+ panic!( "Received hangup signal, exiting..." ) ;
1244+ } ,
1245+ _ = sigpipe. recv( ) => {
1246+ panic!( "Received pipe signal, exiting..." ) ;
1247+ } ,
1248+ }
1249+ } ) ;
1250+
1251+ Ok ( shielded)
1252+ }
12001253}
0 commit comments