Repository navigation
Fix nightly startup crash #4318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
64239f9
Fix nightly startup crash
lawrencecchen e305c73
Address launch smoke review feedback
lawrencecchen 391914b
Tighten startup breadcrumb logging
lawrencecchen b76b500
Harden Ghostty startup callback routing
lawrencecchen 0fb89e6
Fix smoke verification edge cases
lawrencecchen 7ee42f6
Address final launch review feedback
lawrencecchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import Darwin | ||
| import Foundation | ||
| import os | ||
|
|
||
| enum StartupBreadcrumbLog { | ||
| private static let maxFieldLength = 240 | ||
| private nonisolated static let logger = Logger(subsystem: "com.cmuxterm.app", category: "StartupBreadcrumbLog") | ||
| private static let reservedFieldKeys: Set<String> = [ | ||
| "timestamp", | ||
| "event", | ||
| "pid", | ||
| "bundleIdentifier", | ||
| "appVersion", | ||
| "build" | ||
| ] | ||
|
|
||
| static func append(_ event: String, fields: [String: String] = [:]) { | ||
| guard isEnabled else { return } | ||
|
|
||
| var payload: [String: Any] = [ | ||
| "timestamp": ISO8601DateFormatter().string(from: Date()), | ||
| "event": event, | ||
| "pid": ProcessInfo.processInfo.processIdentifier, | ||
| "bundleIdentifier": Bundle.main.bundleIdentifier ?? "unknown", | ||
| "appVersion": Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "unknown", | ||
| "build": Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "unknown" | ||
| ] | ||
|
|
||
| for (key, value) in fields { | ||
| let payloadKey = reservedFieldKeys.contains(key) ? "custom_\(key)" : key | ||
| payload[payloadKey] = sanitized(value) | ||
| } | ||
|
|
||
| do { | ||
| let url = logURL | ||
| try FileManager.default.createDirectory( | ||
| at: url.deletingLastPathComponent(), | ||
| withIntermediateDirectories: true | ||
| ) | ||
| if !FileManager.default.fileExists(atPath: url.path) { | ||
| FileManager.default.createFile(atPath: url.path, contents: nil) | ||
| } | ||
| let line = try JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) | ||
| let handle = try FileHandle(forWritingTo: url) | ||
| defer { try? handle.close() } | ||
| guard flock(handle.fileDescriptor, LOCK_EX) == 0 else { | ||
| let code = POSIXErrorCode(rawValue: errno) ?? .EIO | ||
| throw POSIXError(code) | ||
| } | ||
| defer { flock(handle.fileDescriptor, LOCK_UN) } | ||
| // Startup breadcrumbs are synchronous so the last edge survives immediate launch aborts. | ||
| try handle.seekToEnd() | ||
| try handle.write(contentsOf: line) | ||
| try handle.write(contentsOf: Data([0x0A])) | ||
| } catch { | ||
| logger.fault("cmux startup breadcrumb failed: \(String(describing: error), privacy: .public)") | ||
| } | ||
|
lawrencecchen marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static var isEnabled: Bool { | ||
| let environment = ProcessInfo.processInfo.environment | ||
| if environment["CMUX_DISABLE_STARTUP_BREADCRUMBS"] == "1" { | ||
| return false | ||
| } | ||
| if environment["CMUX_STARTUP_BREADCRUMBS"] == "1" { | ||
| return true | ||
| } | ||
| let bundleIdentifier = Bundle.main.bundleIdentifier ?? "" | ||
| return bundleIdentifier == "com.cmuxterm.app.nightly" | ||
| || bundleIdentifier.hasPrefix("com.cmuxterm.app.nightly.") | ||
| || bundleIdentifier == "com.cmuxterm.app.debug" | ||
| || bundleIdentifier.hasPrefix("com.cmuxterm.app.debug.") | ||
| } | ||
|
|
||
| private static var logURL: URL { | ||
| let logsDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask) | ||
| .first? | ||
| .appendingPathComponent("Logs/cmux", isDirectory: true) | ||
| ?? URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) | ||
| .appendingPathComponent("cmux-logs", isDirectory: true) | ||
| let sanitizedBundleIdentifier = logFileComponent(Bundle.main.bundleIdentifier ?? "unknown") | ||
| return logsDirectory.appendingPathComponent("startup-\(sanitizedBundleIdentifier).log") | ||
| } | ||
|
|
||
| private static func logFileComponent(_ value: String) -> String { | ||
| let allowed = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-") | ||
| return sanitized(value, maxLength: 160).unicodeScalars.map { scalar in | ||
| allowed.contains(scalar) ? String(scalar) : "-" | ||
| }.joined() | ||
| } | ||
|
|
||
| private static func sanitized(_ value: String, maxLength: Int = maxFieldLength) -> String { | ||
| let flattened = value | ||
| .replacingOccurrences(of: "\n", with: "\\n") | ||
| .replacingOccurrences(of: "\r", with: "\\r") | ||
| if flattened.count <= maxLength { | ||
| return flattened | ||
| } | ||
| return String(flattened.prefix(maxLength)) + "...<truncated>" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.