Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/remove-initialized-flag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@iqai/adk": patch
---

refactor: remove redundant initialized flag from DatabaseSessionService
10 changes: 1 addition & 9 deletions packages/adk/src/sessions/database-session-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export interface DatabaseSessionServiceConfig {

export class DatabaseSessionService extends BaseSessionService {
private db: Kysely<Database>;
private initialized = false;
private initPromise: Promise<void> | null = null;

constructor(config: DatabaseSessionServiceConfig) {
Expand All @@ -95,9 +94,6 @@ export class DatabaseSessionService extends BaseSessionService {
* Uses promise deduplication so concurrent callers share a single initialization.
*/
private initializeDatabase(): Promise<void> {
if (this.initialized) {
return Promise.resolve();
}
if (this.initPromise) {
return this.initPromise;
}
Expand All @@ -107,7 +103,6 @@ export class DatabaseSessionService extends BaseSessionService {

// Clear the promise on failure to allow retries.
promise.catch(() => {
// Avoid race conditions if a new initialization has started.
if (this.initPromise === promise) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The comment explaining this conditional check was removed. This check is crucial for handling a subtle race condition, and its purpose is not immediately obvious. Removing the comment harms maintainability, as a future developer might not understand its importance and could mistakenly remove it, introducing a bug.

Please restore a comment explaining why this check is necessary.

			// This check prevents a race condition where a new initialization may have started
			// before this catch block executes for a failed one.
			if (this.initPromise === promise) {

this.initPromise = null;
}
Expand Down Expand Up @@ -204,7 +199,6 @@ export class DatabaseSessionService extends BaseSessionService {
.column("user_id")
.execute();

this.initialized = true;
} catch (error) {
console.error("Error initializing database:", error);
throw error;
Expand All @@ -215,9 +209,7 @@ export class DatabaseSessionService extends BaseSessionService {
* Ensure database is initialized before any operation
*/
private async ensureInitialized(): Promise<void> {
if (!this.initialized) {
await this.initializeDatabase();
}
await this.initializeDatabase();
}

private generateSessionId(): string {
Expand Down
Loading