Skip to content

Commit 27539ae

Browse files
authored
runtime: fix race in yield_defers_until_park test (#6809)
1 parent ea6d652 commit 27539ae

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

tokio/tests/rt_common.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,9 @@ rt_test! {
785785
barrier.wait();
786786

787787
let (fail_test, fail_test_recv) = oneshot::channel::<()>();
788-
788+
let flag_clone = flag.clone();
789789
let jh = tokio::spawn(async move {
790-
// Create a TCP litener
790+
// Create a TCP listener
791791
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
792792
let addr = listener.local_addr().unwrap();
793793

@@ -798,7 +798,7 @@ rt_test! {
798798

799799
// Yield until connected
800800
let mut cnt = 0;
801-
while !flag.load(SeqCst){
801+
while !flag_clone.load(SeqCst){
802802
tokio::task::yield_now().await;
803803
cnt += 1;
804804

@@ -814,7 +814,7 @@ rt_test! {
814814
},
815815
async {
816816
let _ = listener.accept().await.unwrap();
817-
flag.store(true, SeqCst);
817+
flag_clone.store(true, SeqCst);
818818
}
819819
);
820820
});
@@ -824,6 +824,11 @@ rt_test! {
824824
let success = fail_test_recv.await.is_err();
825825

826826
if success {
827+
// Setting flag to true ensures that the tasks we spawned at
828+
// the beginning of the test will exit.
829+
// If we don't do this, the test will hang since the runtime waits
830+
// for all spawned tasks to finish when dropping.
831+
flag.store(true, SeqCst);
827832
// Check for panics in spawned task.
828833
jh.abort();
829834
jh.await.unwrap();

0 commit comments

Comments
 (0)