@@ -63,11 +63,35 @@ func testNestedCallsTask() async {
63
63
// CHECK: Chunk at {{0x[0-9a-fA-F]*}} length {{[1-9[[0-9]*}} kind {{[0-9]*}}
64
64
}
65
65
66
+ // ###HACK: Avoid a race condition by waiting a bit.
67
+ //
68
+ // If we reflect the current task immediately, there is a chance that the
69
+ // runtime is busy manipulating the task structure (e.g. allocating using the
70
+ // task allocator on a child task, or adding a child task) in parallel with
71
+ // the reflection. This can cause this test to crash when the
72
+ // swift-reflection-test binary asks to read from an uninitialized address.
73
+ //
74
+ // While we could stop the crashing by changing the pipe memory reader to
75
+ // return error codes when it is unable to read memory, that would still
76
+ // result in this test failing randomly. It's not obvious what the "proper"
77
+ // fix for this should be; the Concurrency runtime isn't going to export
78
+ // anything that would help here.
79
+ func dodgeRaceCondition( ) async {
80
+ if #available( SwiftStdlib 5 . 7 , * ) {
81
+ try ! await Task . sleep ( for: . milliseconds( 250 ) )
82
+ } else {
83
+ fatalError ( " This test shouldn't be running against old stdlibs. " )
84
+ }
85
+ }
86
+
66
87
func testOneAsyncLet( ) async {
67
88
reflectionLog ( str: " testOneAsyncLet " )
68
89
// CHECK: testOneAsyncLet
69
90
70
91
async let alet = sleepForever ( )
92
+
93
+ await dodgeRaceCondition ( )
94
+
71
95
reflect ( asyncTask: _getCurrentTaskShim ( ) )
72
96
// CHECK: Async task {{0x[0-9a-fA-F]*}}
73
97
// CHECK: children = {
@@ -81,6 +105,9 @@ func testMultipleAsyncLet() async {
81
105
82
106
async let alet1 = sleepForever ( )
83
107
async let alet2 = sleepForever ( )
108
+
109
+ await dodgeRaceCondition ( )
110
+
84
111
reflect ( asyncTask: _getCurrentTaskShim ( ) )
85
112
// CHECK: Async task {{0x[0-9a-fA-F]*}}
86
113
// CHECK: children = {
0 commit comments