Skip to content

Commit dd24d8e

Browse files
committed
[Runtime] Don't emit signposts until the system is ready.
Emitting a signpost for the first time can trigger lazy setup of the logging system, and doing this in the wrong context can cause deadlocks. Check to see if the logging system is already set up, and only emit signposts if it has been to avoid triggering this. As it's hard to determine if the "is it set up?" function is available in the SDK we're building against, only do this in OS builds, as it's not particularly useful in local builds. rdar://124620772
1 parent 66e3110 commit dd24d8e

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

include/swift/Runtime/TracingCommon.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222
#include "swift/Runtime/Config.h"
2323
#include <os/signpost.h>
2424

25-
extern const char *__progname;
25+
extern "C" const char *__progname;
26+
27+
// This function may not be present when building at desk, and isn't really
28+
// needed there, so just skip it in that case.
29+
#if SWIFT_BNI_OS_BUILD
30+
extern "C" bool _os_trace_lazy_init_completed_4swift(void);
31+
#endif
2632

2733
namespace swift {
2834
namespace runtime {
@@ -40,6 +46,15 @@ static inline bool shouldEnableTracing() {
4046
return true;
4147
}
4248

49+
static inline bool tracingReady() {
50+
#if SWIFT_BNI_OS_BUILD
51+
if (!_os_trace_lazy_init_completed_4swift())
52+
return false;
53+
#endif
54+
55+
return true;
56+
}
57+
4358
} // namespace trace
4459
} // namespace runtime
4560
} // namespace swift

stdlib/public/Concurrency/TracingSignpost.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#if SWIFT_STDLIB_CONCURRENCY_TRACING
1818

1919
#include "TracingSignpost.h"
20-
#include "swift/Runtime/TracingCommon.h"
2120
#include <stdio.h>
2221

2322
#define SWIFT_LOG_CONCURRENCY_SUBSYSTEM "com.apple.swift.concurrency"

stdlib/public/Concurrency/TracingSignpost.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Basic/Lazy.h"
2424
#include "swift/Runtime/Casting.h"
2525
#include "swift/Runtime/HeapObject.h"
26+
#include "swift/Runtime/TracingCommon.h"
2627
#include <inttypes.h>
2728
#include <os/log.h>
2829
#include <os/signpost.h>
@@ -79,6 +80,8 @@ void setupLogs(void *unused);
7980
// optimized out.
8081
#define ENSURE_LOGS(...) \
8182
do { \
83+
if (!runtime::trace::tracingReady()) \
84+
return __VA_ARGS__; \
8285
swift::once(LogsToken, setupLogs, nullptr); \
8386
if (!TracingEnabled) \
8487
return __VA_ARGS__; \

stdlib/public/runtime/Tracing.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "Tracing.h"
18-
#include "swift/Runtime/TracingCommon.h"
1918

2019
#if SWIFT_STDLIB_TRACING
2120

stdlib/public/runtime/Tracing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/StringRef.h"
2121
#include "swift/ABI/Metadata.h"
2222
#include "swift/Demangling/Demangler.h"
23+
#include "swift/Runtime/TracingCommon.h"
2324

2425
#if SWIFT_STDLIB_TRACING
2526
#include <os/signpost.h>
@@ -49,6 +50,8 @@ void setupLogs(void *unused);
4950
// optimized out.
5051
#define ENSURE_LOG(log) \
5152
do { \
53+
if (!tracingReady()) \
54+
return {}; \
5255
swift::once(LogsToken, setupLogs, nullptr); \
5356
if (!TracingEnabled) \
5457
return {}; \

0 commit comments

Comments
 (0)