fix: Refactored computed property to avoid force-unwrap #86
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.
Description ✏️
We are encountering fatal errors caused by the force-unwrap used in the
observers
computed property.This occurs due to a race condition where asynchronous code attempts to read the count of observers while some of the observers are being deallocated.
This issue was identified during unit tests, where it would occasionally cause random test failures.
Cause 🤔
The current implementation uses the
filter
function to ensure themap
function can unwrap the optional observer property. However, this approach allows observers to be deallocated between the execution of these two functions. If deallocation occurs during this window, the force-unwrap results in a fatal error.Solution 👨💻
The
observers
property has been refactored to use thecompactMap
function instead offilter
andmap
. This eliminates the need for a force-unwrap and prevents the error.Due to the timing-specific nature of the issue, I have not added unit tests to demonstrate the impact. However, after applying this change, the problem no longer occurs in our unit tests.