clawwork-ai / clawwork-ai/ClawWork

[Bug] Race condition in task-store hydrate with cachedDeviceId

Open
#239 1 comment 0 reactions 0 assignees View on GitHub
area/core
Dominant language
TypeScript
Stars
532
Forks
75
Avg merge
5h 31m
Merged PRs (30d)
1

Description

## Problem

Module-level mutable variable `cachedDeviceId` creates a race condition when `hydrate()` is called concurrently. Multiple calls can overwrite each other's state.

## Location

**File:** `packages/core/src/stores/task-store.ts:95`

**Code:**
```typescript
let cachedDeviceId: string | null = null;

// In hydrate():
if (get().hydrated) return;
cachedDeviceId = await deps.getDeviceId();
```

## Fix Approach

Use a hydration promise to ensure single execution:

```typescript
let hydrationPromise: Promise | null = null;

// In hydrate:
if (get().hydrated) return;
if (hydrationPromise) return hydrationPromise;

hydrationPromise = (async () => {
cachedDeviceId = await deps.getDeviceId();
// ... rest of hydration
})();

return hydrationPromise;
```

Or move `cachedDeviceId` into store state.

## Verification

1. Run `pnpm check` — must pass
2. Test concurrent hydrate calls don't race

## Context

- **WG:** Task & Session Core
- **Priority:** Low (good first issue)
- **Estimated effort:** 15-30 minutes

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.