Skip to content

Commit

Permalink
remove Statsig.shutdown() (vercel#791)
Browse files Browse the repository at this point in the history
### Description

Removes `Statsig.shutdown()` from middleware.

Calling Statsig.shutdown leads to a race condition when multiple
requests happen to the same middleware:
- Request A calls `await Statsig.initialize()`
- Request B calls `await Statsig.initialize()`
- Request A calls `Statsig.shutdown()` destroying the singleton
- Request B tries to use Statsig but the singleton is no longer
initialized

This leads to the following error being thrown

```
Error: Call and wait for initialize() to finish first.
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/Errors.js:22:27)
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/StatsigServer.js:532:27)
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/StatsigServer.js:523:30)
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/StatsigServer.js:463:22)
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/StatsigServer.js:224:70)
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/ErrorBoundary.js:27:25)
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/StatsigServer.js:224:35)
    at (node_modules/.pnpm/[email protected]/node_modules/statsig-node/dist/index.js:160:37)
    at (middleware.ts:33:27)
    at (node_modules/.pnpm/[email protected][email protected][email protected]/node_modules/next/dist/esm/server/web/adapter.js:150:19)
```
  • Loading branch information
dferber90 authored Sep 15, 2023
1 parent 77524d9 commit 01f237d
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions edge-middleware/ab-testing-statsig/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ export async function middleware(req: NextRequest, event: NextFetchEvent) {
}

// Flush exposure logs to Statsig
event.waitUntil(
Statsig.flush().then(() => {
Statsig.shutdown()
})
)
event.waitUntil(Statsig.flush())

return res
}

0 comments on commit 01f237d

Please sign in to comment.