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

fix: move session-specific logic from ContextPlugin to avoid race conditions #86

Merged
merged 3 commits into from
Oct 18, 2022

Conversation

falconandy
Copy link
Contributor

Summary

Session-specific logic moved from ContextPlugin to avoid race conditions

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: No

@secure-code-warrior-for-github

Micro-Learning Topic: Race condition (Detected by phrase)

Matched on "race condition"

What is this? (2min video)

A race condition is a flaw that produces an unexpected result when the timing of actions impact other actions.

Try a challenge in Secure Code Warrior

@falconandy falconandy force-pushed the DXOC-261-sessions-weird-behavior branch from aa11870 to 5973986 Compare October 14, 2022 16:52
@justin-fiedler justin-fiedler self-requested a review October 14, 2022 18:23
Copy link
Contributor

@qingzhuozhen qingzhuozhen left a comment

Choose a reason for hiding this comment

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

Maybe we could add a simple test for this?

sendSessionEvent(END_SESSION_EVENT)
val options = EventOptions()
options.timestamp = if (lastEventTime > 0) lastEventTime + 1 else null
sendSessionEvent(END_SESSION_EVENT, options)
Copy link
Contributor

Choose a reason for hiding this comment

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

lastEventTime + 1 is for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure how events are ordered in UI. I added 1 millisecond to ensure the session_end event is last. Maybe it is unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, I guess it won't hurt much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to lastEventTime

override fun processEvent(event: BaseEvent) {
val eventTimestamp = event.timestamp ?: System.currentTimeMillis()
event.timestamp = eventTimestamp
if (lastEventTime < eventTimestamp) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this will break the logic for isWithinMinTimeBetweenSessions if the event falls into the later startNewSessionIfNeeded, maybe we should move this to the bottom

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed that block - it looks like refreshSessionTime already does it

if (lastEventTime < eventTimestamp) {
    lastEventTime = eventTimestamp
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@qingzhuozhen
I am thinking about tests

@falconandy
Copy link
Contributor Author

@qingzhuozhen
A test was added. I need to change some session-related logic to make it more deterministic to be able to test it.

sendSessionEvent(END_SESSION_EVENT)
val options = EventOptions()
options.timestamp = if (lastEventTime > 0) lastEventTime + 1 else null
sendSessionEvent(END_SESSION_EVENT, options)
Copy link
Contributor

Choose a reason for hiding this comment

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

I see, I guess it won't hurt much.

val sessionEndEvent = BaseEvent()
sessionEndEvent.eventType = END_SESSION_EVENT
sessionEndEvent.timestamp = if (lastEventTime > 0) lastEventTime else null
sessionEndEvent.sessionId = sessionId
Copy link
Contributor

Choose a reason for hiding this comment

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

if we set the eventId here, I guess it could also be fine?

if (configuration.optOut) {
logger.info("Skip event for opt out config.")
return
}

val sessionEvents = processEvent(event)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not call it sessionEvents? just events are fine. This way seems to be coupling the logic in core and android package.

@qingzhuozhen qingzhuozhen merged commit 311c230 into main Oct 18, 2022
@qingzhuozhen qingzhuozhen deleted the DXOC-261-sessions-weird-behavior branch October 18, 2022 23:49
github-actions bot pushed a commit that referenced this pull request Oct 19, 2022
## [1.4.5](v1.4.4...v1.4.5) (2022-10-19)

### Bug Fixes

* move session-specific logic from ContextPlugin to avoid race conditions ([#86](#86)) ([311c230](311c230))
@github-actions
Copy link

🎉 This PR is included in version 1.4.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants