Skip to content

Commit 279dde1

Browse files
authored
Merge branch 'master' into 18446-knowledge-workshop-investigate-teams-auth-persistence-repeated-relinking
2 parents 3fe17ee + ac9d4ce commit 279dde1

6 files changed

Lines changed: 472 additions & 281 deletions

File tree

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
3-
"version": "3.36.2",
3+
"version": "3.36.3",
44
"npmClient": "yarn",
55
"concurrency": 20,
66
"command": {

packages/backend-core/src/redis/redlockImpl.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ export async function doWithLock<T>(
120120
): Promise<RedlockExecution<T>> {
121121
const redlock = await getClient(opts.type, opts.customOptions)
122122
let lock: Redlock.Lock | undefined
123-
let timeout
123+
let timeout: ReturnType<typeof setTimeout> | undefined
124+
let inflightExtend: Promise<void> | undefined
125+
let stopped = false
124126
try {
125127
const name = getLockName(opts)
126128

@@ -134,8 +136,17 @@ export async function doWithLock<T>(
134136
// We keep extending the lock while the task is running
135137
const extendInIntervals = (): void => {
136138
timeout = setTimeout(async () => {
137-
lock = await lock!.extend(ttl, () => opts.onExtend && opts.onExtend())
138-
139+
if (stopped) {
140+
return
141+
}
142+
inflightExtend = (async () => {
143+
lock = await lock!.extend(
144+
ttl,
145+
() => opts.onExtend && opts.onExtend()
146+
)
147+
})()
148+
await inflightExtend
149+
inflightExtend = undefined
139150
extendInIntervals()
140151
}, ttl / 2)
141152
}
@@ -161,7 +172,12 @@ export async function doWithLock<T>(
161172
throw e
162173
}
163174
} finally {
175+
stopped = true
164176
clearTimeout(timeout)
177+
// If an extend is in-flight, wait for it to settle so that `lock` holds the
178+
// latest token before we call unlock. Extend errors are swallowed here
179+
// because the lock will expire on its own if unlock also fails.
180+
await inflightExtend?.catch(() => {})
165181
await lock?.unlock()
166182
}
167183
}

0 commit comments

Comments
 (0)