@@ -502,7 +502,7 @@ impl Runtime {
502
502
handles_capacity : usize ,
503
503
) -> Result < Option < ReadStatus > , OakStatus > {
504
504
self . validate_handle_access ( node_id, reference) ?;
505
- self . channels
505
+ let result = self . channels
506
506
. with_channel ( self . channels . get_reader_channel ( reference) ?, |channel| {
507
507
let mut messages = channel. messages . write ( ) . unwrap ( ) ;
508
508
match messages. front ( ) {
@@ -517,7 +517,6 @@ impl Runtime {
517
517
ReadStatus :: NeedsCapacity ( req_bytes_capacity, req_handles_capacity)
518
518
} else {
519
519
let msg = messages. pop_front ( ) . expect ( "Front element disappeared while we were holding the write lock!" ) ;
520
- self . track_handles_in_node ( node_id, msg. channels . clone ( ) ) ;
521
520
ReadStatus :: Success ( msg)
522
521
} ,
523
522
) )
@@ -530,7 +529,15 @@ impl Runtime {
530
529
}
531
530
}
532
531
}
533
- } )
532
+ } ) ;
533
+
534
+ // Add handles outside the channels lock so we don't hold the node lock inside the channel
535
+ // lock.
536
+ if let Ok ( Some ( ReadStatus :: Success ( ref msg) ) ) = result {
537
+ self . track_handles_in_node ( node_id, msg. channels . clone ( ) ) ;
538
+ }
539
+
540
+ result
534
541
}
535
542
536
543
/// Return the direction of a [`Handle`]. This is useful when reading
@@ -605,6 +612,13 @@ impl Runtime {
605
612
. remove ( & node_id)
606
613
. expect ( "remove_node_id: Node didn't exist!" ) ;
607
614
}
615
+
616
+ /// Add an [`NodeId`] [`Node`] pair to the [`Runtime`]. This method temporarily holds the node
617
+ /// write lock.
618
+ fn add_running_node ( & self , reference : NodeId , node : Node ) {
619
+ let mut nodes = self . nodes . write ( ) . unwrap ( ) ;
620
+ nodes. insert ( reference, node) ;
621
+ }
608
622
}
609
623
610
624
/// A reference to a [`Runtime`].
@@ -639,7 +653,6 @@ impl RuntimeRef {
639
653
// to do that we first need to provide a reference to the caller node as a parameter to this
640
654
// function.
641
655
642
- let mut nodes = self . nodes . write ( ) . unwrap ( ) ;
643
656
let reference = self . new_node_reference ( ) ;
644
657
645
658
let reader = self . channels . duplicate_reference ( reader) ?;
@@ -666,7 +679,7 @@ impl RuntimeRef {
666
679
667
680
// If the node was successfully started, insert it in the list of currently running
668
681
// nodes.
669
- nodes . insert (
682
+ self . add_running_node (
670
683
reference,
671
684
Node {
672
685
reference,
0 commit comments