-
Notifications
You must be signed in to change notification settings - Fork 52
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
Catch race condition in PostHogFileBackedQueue.deleteFiles #218
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -100,10 +100,14 @@ class PostHogFileBackedQueue { | |||
|
||||
private func deleteFiles(_ count: Int) { | ||||
for _ in 0 ..< count { | ||||
if items.isEmpty { return } | ||||
let removed = items.remove(at: 0) // We always remove from the top of the queue | ||||
|
||||
deleteSafely(queue.appendingPathComponent(removed)) | ||||
if let removed: String = _items.mutate({ items in | ||||
if items.isEmpty { | ||||
return nil | ||||
} | ||||
return items.remove(at: 0) // We always remove from the top of the queue | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did you reproduce this issue since the whole block is locked with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this call here is outside the func add(_ event: PostHogEvent) {
if fileQueue.depth >= config.maxQueueSize {
hedgeLog("Queue is full, dropping oldest event")
// first is always oldest
fileQueue.delete(index: 0)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm in this case we might have a bug deleting the wrong file as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we definitely shouldn't wait on a lock here. Thing is that we can't assume that this is aways called on main thread either. Maybe it's best to try and recreate this crash with unit tests with background queue and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking the thread is easy ( posthog-ios/PostHog/PostHogQueue.swift Line 212 in 1bb1709
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fwiw I only got one crash report with this call stack. |
||||
}) { | ||||
deleteSafely(queue.appendingPathComponent(removed)) | ||||
} | ||||
} | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L49 does something similar, maybe this patch has to be applied there as well.