Description
Description
I have a reproducible case of top level let
declarations randomly causing crashes.
The project is a simple Swift CLI tool, using https://github.com/pointfreeco/swift-parsing .
You can find the specific branch with the problem here https://github.com/alexito4/kindle-highlights/tree/swift-crash-repo and a conversation on the swift-parsing repo pointfreeco/swift-parsing#275
I've discarded pretty much anything else I could, there is another branch trying-fixes
where I removed everything I could and still crashes, albeit at different points (which to me points to some memory issue since every slight change in the program makes it crash differently)
Steps to reproduce
- Checkout https://github.com/alexito4/kindle-highlights.git
- Switch to the
swift-crash-repo
branch - Run
swift run -c release kindle-highlights ./short.txt
and see it crash with6993 bus error
❌ - Run (in debug)
swift run kindle-highlights ./short.txt
and it works fine (at least on my machine) ✅ - You can also open Xcode
xed .
and run the tests, you will get aThread 1: EXC_BAD_ACCESS
❌ Note that some tests also fail (before crashing) due to what seems some bad memory when manipulating the strings.
Expected behavior
It shouldn't crash.
Screenshots
Crash running tests trough Xcode:
Crash running with release config:
Environment
- Swift compiler version info:
swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51) Target: arm64-apple-macosx13.0
- Xcode version info:
Version 14.2 (14C18)
- Deployment target:
macOS 13.1 (22C65)
I've also tried running it in Docker with the same results. Docker Image swift-5.7.2
platform ubuntu22.04
Additional context
After being pointed to an issue with top level declarations pointfreeco/swift-parsing#275 (comment) I switched the code to use functions and create the parsers instead of storing them in top level let
s and all the problems have gone away. (the diff is here alexito4/kindle-highlights@c82ce47)