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

RUM-3175 Fix possible file name conflict during consent change #2113

Merged

Conversation

ncreated
Copy link
Member

What and why?

📦 This PR addresses a potential file name conflict issue during tracking consent changes.

Initially considered a flakiness ❄️ in testWhenWritingEventsWithPendingConsentThenGranted_itUploadsAllEvents() (added in #2063), it was discovered to be a real issue that can occur in certain edge cases.

The conflict happens when files are moved between .pending and .granted folders and events are written immediately before and after the consent change. If this occurs very quickly (under 1ms), the last batch in the pending folder may be named the same as the next batch in the granted folder. Since the SDK does not check if the file exists, the migrated file gets overwritten by the new event:

T - timestamp

[T0] consent: PENDING
[T1] write event A --> batch file created: intermediate/T1
[T1] change consent to GRANTED --> move intermediate/T1 to authorized/T1
[T1] write event B --> batch file created: authorized/T1 
                                           ^ 🐞 The previous T1 with event A is deleted
                                            and replaced with the new batch for event B.

How?

This fix ensures that batch file names are unique, even in the edge case above, by waiting and generating a new file name when a conflict is detected. It guarantees that the new file's timestamp will be at least one precision interval (1ms) later than the existing file, preventing conflicts during consent changes.

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference
  • Add CHANGELOG entry for user facing changes
  • Add Objective-C interface for public APIs (see our guidelines [internal]) and run make api-surface)

@ncreated ncreated self-assigned this Nov 15, 2024
@ncreated ncreated changed the title RUM-3175 Fix possible batch file name conflict during tracking consent change RUM-3175 Fix possible batch file name conflict during consent change Nov 15, 2024
@ncreated ncreated changed the title RUM-3175 Fix possible batch file name conflict during consent change RUM-3175 Fix possible file name conflict during consent change Nov 15, 2024
@ncreated ncreated force-pushed the ncreated/RUM-3175/fix-batch-name-conflict-on-consent-change branch from ecb178e to fe7d709 Compare November 15, 2024 18:09
@ncreated ncreated marked this pull request as ready for review November 15, 2024 18:09
@ncreated ncreated requested review from a team as code owners November 15, 2024 18:09
…t change

Addressed potential conflicts that could occur when files are moved between
unauthorized (.pending) and authorized (.granted) folders during tracking consent
changes. Ensured that new file names are generated with the correct precision to avoid
conflicts in real-device scenarios.
@ncreated ncreated force-pushed the ncreated/RUM-3175/fix-batch-name-conflict-on-consent-change branch from fe7d709 to 0740e49 Compare November 15, 2024 18:13
@datadog-datadog-prod-us1
Copy link

datadog-datadog-prod-us1 bot commented Nov 15, 2024

Datadog Report

Branch report: ncreated/RUM-3175/fix-batch-name-conflict-on-consent-change
Commit report: b6dee1c
Test service: dd-sdk-ios

✅ 0 Failed, 3545 Passed, 0 Skipped, 2m 21.69s Total Time
🔻 Test Sessions change in coverage: 4 decreased, 5 increased, 5 no change

🔻 Code Coverage Decreases vs Default Branch (4)

  • test DatadogInternalTests tvOS 79.7% (-0.08%) - Details
  • test DatadogSessionReplayTests iOS 33.21% (-0.03%) - Details
  • test DatadogTraceTests tvOS 54.28% (-0.02%) - Details
  • test DatadogCrashReportingTests tvOS 26.67% (-0.02%) - Details

Copy link
Member

@mariedm mariedm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job identifying this issue! 💪

var newFileName = fileNameFrom(fileCreationDate: dateProvider.now)
while directory.hasFile(named: newFileName) {
// Wait for the precision duration to avoid generating the same file name
Thread.sleep(forTimeInterval: Constants.fileNamePrecision)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this scenario is rare, but instead of blocking the thread, could we consider appending a random ID to ensure file name uniqueness? Or is a date in the filename required for identification purposes or another logic?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not a fan of adding a sleep here. Since the date is used to sort batches at upload, can't we just add a 1ms to the dateProvider.now instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I've updated the code to add 1ms in case of a conflict 👍 ✅.

@mariedm, appending a random ID could have more indirect consequences here. On upload trigger, we read the recent batches and sort them by date. Introducing an ID would make the ordering ambiguous if two timestamps are the same, which would require additional code changes to handle these edge cases. For that reason, adjusting the timestamp seems like a less impactful solution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I agree, using a random ID doesn't make sense.

do not block the I/O thread if name conflict is detected,
instead add precision interval to generate new name
Copy link
Member

@maxep maxep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!

@ncreated ncreated merged commit 52842a9 into develop Nov 19, 2024
15 checks passed
@ncreated ncreated deleted the ncreated/RUM-3175/fix-batch-name-conflict-on-consent-change branch November 19, 2024 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants