Skip to content
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

chore: delete queue files if serialization fails #232

Merged
merged 3 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next

- feat: support reuse of `anonymousId` between user changes ([#229](https://github.com/PostHog/posthog-android/pull/229))
- chore: delete queue files if serialization fails ([#232](https://github.com/PostHog/posthog-android/pull/232))

## 3.11.3 - 2025-02-26

Expand Down
30 changes: 23 additions & 7 deletions posthog/src/main/java/com/posthog/internal/PostHogQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,23 @@ internal class PostHogQueue(
deque.add(file)
}

var hasPendingEvents = true
try {
val os = config.encryption?.encrypt(file.outputStream()) ?: file.outputStream()
os.use { theOutputStream ->
config.serializer.serialize(event, theOutputStream.writer().buffered())
}
config.logger.log("Queued Event ${event.event}: ${file.name}.")

flushIfOverThreshold()
} catch (e: Throwable) {
hasPendingEvents = false
config.logger.log("Event ${event.event}: ${file.name} failed to parse: $e.")

// if for some reason the file failed to serialize, lets delete it
file.deleteSafely(config)
}

if (hasPendingEvents) {
flushIfOverThreshold()
}
}
}
Expand Down Expand Up @@ -163,6 +170,17 @@ internal class PostHogQueue(
}
}

private fun deleteFileSafely(
file: File,
throwable: Throwable? = null,
) {
synchronized(dequeLock) {
deque.remove(file)
}
file.deleteSafely(config)
config.logger.log("File: ${file.name} failed to parse: $throwable.")
}

@Throws(PostHogApiError::class, IOException::class)
private fun batchEvents() {
val files = takeFiles()
Expand All @@ -175,14 +193,12 @@ internal class PostHogQueue(
val event = config.serializer.deserialize<PostHogEvent?>(it.reader().buffered())
event?.let { theEvent ->
events.add(theEvent)
} ?: run {
deleteFileSafely(file)
}
}
} catch (e: Throwable) {
synchronized(dequeLock) {
deque.remove(file)
}
file.deleteSafely(config)
config.logger.log("File: ${file.name} failed to parse: $e.")
deleteFileSafely(file, e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ internal class PostHogSendCachedEventsIntegration(
event?.let {
events.add(event)
eventsCount++
} ?: run {
removeFileSafely(iterator)
}
}
} catch (e: Throwable) {
iterator.remove()
config.logger.log("Event failed to parse: $e.")
removeFileSafely(iterator, e)
}
// stop the while loop since the batch is full
if (events.size >= config.maxBatchSize) {
Expand Down Expand Up @@ -135,6 +136,24 @@ internal class PostHogSendCachedEventsIntegration(
}
}

private fun deleteFileSafely(
file: File,
iterator: MutableIterator<File>,
throwable: Throwable? = null,
) {
config.logger.log("File: ${file.name} failed to parse: $throwable.")
iterator.remove()
file.deleteSafely(config)
}

private fun removeFileSafely(
iterator: MutableIterator<ByteArray>,
throwable: Throwable? = null,
) {
config.logger.log("Event failed to parse: $throwable.")
iterator.remove()
}

@Throws(PostHogApiError::class, IOException::class)
private fun flushEvents(
storagePrefix: String?,
Expand Down Expand Up @@ -175,12 +194,12 @@ internal class PostHogSendCachedEventsIntegration(
event?.let {
events.add(event)
eventsCount++
} ?: run {
deleteFileSafely(file, iterator)
}
}
} catch (e: Throwable) {
config.logger.log("File: ${file.name} failed to parse: $e.")
iterator.remove()
file.deleteSafely(config)
deleteFileSafely(file, iterator, e)
}

// stop the while loop since the batch is full
Expand Down
Loading