@@ -4,6 +4,7 @@ use lazy_static::lazy_static;
44use  std:: collections:: { BTreeSet ,  HashMap ,  HashSet } ; 
55use  std:: sync:: { Arc ,  RwLock } ; 
66use  std:: time:: Instant ; 
7+ use  tokio:: task; 
78
89use  graph:: components:: ethereum:: { triggers_in_block,  EthereumNetworks } ; 
910use  graph:: components:: store:: { BlockStore ,  ModificationsAndCache } ; 
@@ -358,7 +359,7 @@ where
358359            let  id = manifest. id . clone ( ) ; 
359360
360361            // `start_subgraph_deployment` is blocking. 
361-             tokio :: task:: spawn_blocking ( move  || { 
362+             task:: spawn_blocking ( move  || { 
362363                store
363364                    . start_subgraph_deployment ( & logger,  & id) 
364365                    . map_err ( Error :: from) 
@@ -445,9 +446,15 @@ where
445446        // block stream and include events for the new data sources going 
446447        // forward; this is easier than updating the existing block stream. 
447448        // 
448-         // This task has many calls to the store, so mark it as `blocking`. 
449+         // This is a long-running and unfortunately a blocking future (see #905), so it is run in 
450+         // its own thread. It is also run with `task::unconstrained` because we have seen deadlocks 
451+         // occur without it, possibly caused by our use of legacy futures and tokio versions in the 
452+         // codebase and dependencies, which may not play well with the tokio 1.0 cooperative 
453+         // scheduling. It is also logical in terms of performance to run this with `unconstrained`, 
454+         // it has a dedicated OS thread so the OS will handle the preemption. See 
455+         // https://github.com/tokio-rs/tokio/issues/3493. 
449456        graph:: spawn_thread ( deployment_id. to_string ( ) ,  move  || { 
450-             if  let  Err ( e)  = graph:: block_on ( run_subgraph ( ctx) )  { 
457+             if  let  Err ( e)  = graph:: block_on ( task :: unconstrained ( run_subgraph ( ctx) ) )  { 
451458                error ! ( 
452459                    & logger, 
453460                    "Subgraph instance failed to run: {}" , 
0 commit comments