-
Notifications
You must be signed in to change notification settings - Fork 4
sdk-core, electron: fix endless loop when loading SessionFiles #347
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e937c31
sdk-core: fix endless loop when loading SessionFiles
perf2711 0df5a77
electron: fix sessionId build errors
perf2711 a237613
node: fix unit tests (NFC)
perf2711 65e7828
react-native: fix unit tests (NFC)
perf2711 1117d37
sdk-core: make sessionId backwards compatible
perf2711 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,15 +5,19 @@ import { FileSystem } from './FileSystem.js'; | |
|
|
||
| interface FileSession { | ||
| readonly file: string; | ||
| readonly sessionId: string; | ||
| readonly sessionId: SessionId; | ||
|
Collaborator
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 about backward compatibility?
Contributor
Author
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. You're right, this could fail. I added a check for this where required. |
||
| readonly escapedSessionId: string; | ||
| readonly timestamp: number; | ||
| } | ||
|
|
||
| type SessionEvents = { | ||
| unlocked: []; | ||
| }; | ||
|
|
||
| export interface SessionId { | ||
| readonly id: string; | ||
| readonly timestamp: number; | ||
| } | ||
|
|
||
| const SESSION_MARKER_PREFIX = 'bt-session'; | ||
|
|
||
| const isDefined = <T>(t: T | undefined): t is T => !!t; | ||
|
|
@@ -30,17 +34,16 @@ export class SessionFiles implements BacktraceModule { | |
| constructor( | ||
| private readonly _fileSystem: FileSystem, | ||
| private readonly _directory: string, | ||
| public readonly sessionId: string, | ||
| public readonly sessionId: SessionId, | ||
| private readonly _maxPreviousLockedSessions = 1, | ||
| public readonly timestamp = Date.now(), | ||
| private readonly _lockable = true, | ||
| ) { | ||
| this._escapedSessionId = this.escapeFileName(sessionId); | ||
| this._escapedSessionId = this.escapeFileName(sessionId.id); | ||
| this.marker = this.getFileName(SESSION_MARKER_PREFIX); | ||
| } | ||
|
|
||
| public initialize(): void { | ||
| this.createSessionMarker(); | ||
| return this.createSessionMarker(); | ||
| } | ||
|
|
||
| public getPreviousSession() { | ||
|
|
@@ -57,12 +60,14 @@ export class SessionFiles implements BacktraceModule { | |
| .filter((f) => f.startsWith(SESSION_MARKER_PREFIX)) | ||
| .map((f) => this.getFileSession(f)) | ||
| .filter(isDefined) | ||
| .sort((a, b) => b.timestamp - a.timestamp); | ||
| .sort((a, b) => b.sessionId.timestamp - a.sessionId.timestamp); | ||
|
|
||
| const currentSessionMarker = sessionMarkers.find((s) => s.sessionId === this.sessionId); | ||
| const currentSessionMarker = sessionMarkers.find((s) => this.sessionIdEquals(s.sessionId, this.sessionId)); | ||
|
|
||
| const lastSessionMarker = currentSessionMarker | ||
| ? sessionMarkers.filter(({ timestamp }) => currentSessionMarker.timestamp > timestamp)[0] | ||
| ? sessionMarkers.filter( | ||
| ({ sessionId }) => currentSessionMarker.sessionId.timestamp > sessionId.timestamp, | ||
| )[0] | ||
| : sessionMarkers[0]; | ||
|
|
||
| if (!lastSessionMarker) { | ||
|
|
@@ -74,15 +79,14 @@ export class SessionFiles implements BacktraceModule { | |
| this._directory, | ||
| lastSessionMarker.sessionId, | ||
| this._maxPreviousLockedSessions - 1, | ||
| lastSessionMarker.timestamp, | ||
| this._maxPreviousLockedSessions > 0, | ||
| )); | ||
| } | ||
|
|
||
| public getSessionWithId(sessionId: string) { | ||
| public getSessionWithId(sessionId: SessionId) { | ||
| // eslint-disable-next-line @typescript-eslint/no-this-alias | ||
| let session: SessionFiles | undefined = this; | ||
| while (session && session.sessionId !== sessionId) { | ||
| while (session && this.sessionIdEquals(session.sessionId, sessionId)) { | ||
| session = session.getPreviousSession(); | ||
| } | ||
|
|
||
|
|
@@ -124,7 +128,11 @@ export class SessionFiles implements BacktraceModule { | |
| public getFileName(prefix: string) { | ||
| this.throwIfCleared(); | ||
|
|
||
| return this._directory + '/' + `${this.escapeFileName(prefix)}_${this._escapedSessionId}_${this.timestamp}`; | ||
| return ( | ||
| this._directory + | ||
| '/' + | ||
| `${this.escapeFileName(prefix)}_${this._escapedSessionId}_${this.sessionId.timestamp}` | ||
| ); | ||
| } | ||
|
|
||
| public getSessionFiles() { | ||
|
|
@@ -134,7 +142,7 @@ export class SessionFiles implements BacktraceModule { | |
| return files | ||
| .map((file) => this.getFileSession(file)) | ||
| .filter(isDefined) | ||
| .filter(({ sessionId }) => sessionId === this.sessionId) | ||
| .filter(({ sessionId }) => this.sessionIdEquals(sessionId, this.sessionId)) | ||
| .map(({ file }) => this._directory + '/' + file); | ||
| } | ||
|
|
||
|
|
@@ -184,7 +192,14 @@ export class SessionFiles implements BacktraceModule { | |
| return undefined; | ||
| } | ||
|
|
||
| return { file, escapedSessionId, timestamp, sessionId: this.unescapeFileName(escapedSessionId) }; | ||
| return { | ||
| file, | ||
| escapedSessionId, | ||
| sessionId: { | ||
| timestamp, | ||
| id: this.unescapeFileName(escapedSessionId), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| private readDirectoryFiles() { | ||
|
|
@@ -232,4 +247,19 @@ export class SessionFiles implements BacktraceModule { | |
| throw new Error('This session files are cleared.'); | ||
| } | ||
| } | ||
|
|
||
| private sessionIdEquals(a: SessionId, b: SessionId) { | ||
| return a.id === b.id && a.timestamp === b.timestamp; | ||
| } | ||
|
|
||
| public static isValidSessionId(value: unknown): value is SessionId { | ||
| return ( | ||
| typeof value === 'object' && | ||
| !!value && | ||
| 'id' in value && | ||
| 'timestamp' in value && | ||
| typeof value.id === 'string' && | ||
| typeof value.timestamp === 'number' | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.