Skip to content

Commit b2bd176

Browse files
authored
[Concurrency] Fix too eager early return in checkIsolation mode detecting (#73495)
1 parent ed1cd6c commit b2bd176

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

include/swift/Runtime/EnvironmentVariables.h

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ SWIFT_RUNTIME_STDLIB_SPI bool concurrencyEnableJobDispatchIntegration();
5353
// Concurrency library can call.
5454
SWIFT_RUNTIME_STDLIB_SPI bool concurrencyValidateUncheckedContinuations();
5555

56+
// Wrapper around SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE that the
57+
// Concurrency library can call.
58+
SWIFT_RUNTIME_STDLIB_SPI const char *concurrencyIsCurrentExecutorLegacyModeOverride();
59+
5660
} // end namespace environment
5761
} // end namespace runtime
5862
} // end namespace swift

stdlib/public/Concurrency/Actor.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/Runtime/Bincompat.h"
3131
#include "swift/Runtime/Casting.h"
3232
#include "swift/Runtime/DispatchShims.h"
33+
#include "swift/Runtime/EnvironmentVariables.h"
3334
#include "swift/Threading/Mutex.h"
3435
#include "swift/Threading/Once.h"
3536
#include "swift/Threading/Thread.h"
@@ -351,15 +352,16 @@ static void checkIsCurrentExecutorMode(void *context) {
351352

352353
// Potentially, override the platform detected mode, primarily used in tests.
353354
#if SWIFT_STDLIB_HAS_ENVIRON
354-
const char *modeStr = getenv("SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE");
355-
if (!modeStr)
356-
return;
357-
358-
if (strcmp(modeStr, "nocrash") == 0) {
359-
useLegacyMode = Legacy_NoCheckIsolated_NonCrashing;
360-
} else if (strcmp(modeStr, "crash") == 0) {
361-
useLegacyMode = Default_UseCheckIsolated_AllowCrash;
362-
} // else, just use the platform detected mode
355+
if (const char *modeStr =
356+
runtime::environment::concurrencyIsCurrentExecutorLegacyModeOverride()) {
357+
if (modeStr) {
358+
if (strcmp(modeStr, "nocrash") == 0) {
359+
useLegacyMode = true;
360+
} else if (strcmp(modeStr, "crash") == 0) {
361+
useLegacyMode = false;
362+
} // else, just use the platform detected mode
363+
}
364+
}
363365
#endif // SWIFT_STDLIB_HAS_ENVIRON
364366
isCurrentExecutorMode = useLegacyMode ? Legacy_NoCheckIsolated_NonCrashing
365367
: Default_UseCheckIsolated_AllowCrash;

stdlib/public/runtime/EnvironmentVariables.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,7 @@ SWIFT_RUNTIME_STDLIB_SPI bool concurrencyEnableJobDispatchIntegration() {
272272
SWIFT_RUNTIME_STDLIB_SPI bool concurrencyValidateUncheckedContinuations() {
273273
return runtime::environment::SWIFT_DEBUG_VALIDATE_UNCHECKED_CONTINUATIONS();
274274
}
275+
276+
SWIFT_RUNTIME_STDLIB_SPI const char *concurrencyIsCurrentExecutorLegacyModeOverride() {
277+
return runtime::environment::SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE();
278+
}

stdlib/public/runtime/EnvironmentVariables.def

+12
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,16 @@ VARIABLE(SWIFT_BACKTRACE, string, "",
112112
"crash catching and backtracing support in the runtime. "
113113
"See docs/Backtracing.rst in the Swift repository for details.")
114114

115+
VARIABLE(SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE, string, "",
116+
"Allows for suppressing 'is current executor' equality check crashes. "
117+
"As since Swift 6.0 checking for current executor equality, may crash "
118+
"and will never return 'false' because we are calling into library "
119+
"implemented SerialExecutor.checkIsolation which should crash if the "
120+
"isolation is not the expected one. Some old code may rely on the "
121+
"non-crashing behavior. This flag enables temporarily restoring the "
122+
"legacy 'nocrash' behavior until adopting code has been adjusted. "
123+
"Legal values are: "
124+
" 'nocrash' (Legacy behavior), "
125+
" 'crash' (Swift 6.0+ behavior)")
126+
115127
#undef VARIABLE

test/abi/macOS/arm64/stdlib.swift

+3
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,6 @@ Added: _swift_updatePureObjCClassMetadata
567567

568568
// Runtime bincompat functions for Concurrency runtime to detect legacy mode
569569
Added: _swift_bincompat_useLegacyNonCrashingExecutorChecks
570+
571+
// Add add SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE
572+
Added: _concurrencyIsCurrentExecutorLegacyModeOverride

test/abi/macOS/x86_64/stdlib.swift

+3
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,6 @@ Added: _swift_updatePureObjCClassMetadata
567567

568568
// Runtime bincompat functions for Concurrency runtime to detect legacy mode
569569
Added: _swift_bincompat_useLegacyNonCrashingExecutorChecks
570+
571+
// Add add SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE
572+
Added: _concurrencyIsCurrentExecutorLegacyModeOverride

0 commit comments

Comments
 (0)