Fix crash dereferencing unowned _publisher in KVO trampoline #112
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.
A
FoilDefaultStorage{,Optional}
can deallocate while itsObserverTrampoline
sticks around long enough to receive a KVO notification. (I'm not sure how.) When this happens, the attempt to dereference the captured_publisher
crashes.I don't know how to write a reliable test for the test suite, but I can reproduce the crash ~instantly with this code (e.g. pasted into a new test) in Xcode 16.0 beta 5 on an iPhone 8 running iOS 16.7.8 (as well as in iOS 15, 16, 17, and 18 simulators I have kicking around):
That's an admittedly silly test, but I'm seeing crashes in the wild attempting to dereference
_publisher
.The obvious fix to me is to capture
_publisher
as either weak or strong, instead of unowned. A strong reference doesn't cause a cycle, so I chose it over the theoretically slower weak reference.