Use sync access for the temp JSON file database
Replace all async reads and writes to the JSON database with the sync reads and writes to prevent a data corruption race condition where two requests are processed at the same time and write to the same file, or one reads while the other writes causing read of partially written data.
This commit is contained in:
parent
f9d188b2ba
commit
80cec71308
23 changed files with 138 additions and 138 deletions
|
@ -18,7 +18,7 @@ export default defineEventHandler(async (event) => {
|
|||
});
|
||||
}
|
||||
|
||||
const users = await readUsers();
|
||||
const users = readUsers();
|
||||
const user = users.find(user => user.id === patch.id);
|
||||
if (!user || user.deleted) {
|
||||
throw createError({
|
||||
|
@ -52,28 +52,28 @@ export default defineEventHandler(async (event) => {
|
|||
user.name = patch.name;
|
||||
}
|
||||
user.updatedAt = new Date().toISOString();
|
||||
await writeUsers(users);
|
||||
writeUsers(users);
|
||||
broadcastEvent({
|
||||
id: await nextEventId(),
|
||||
id: nextEventId(),
|
||||
type: "user-update",
|
||||
data: serverUserToApi(user),
|
||||
});
|
||||
|
||||
// Rotate sessions with the user in it if the access changed
|
||||
if (accessChanged) {
|
||||
const sessions = await readSessions();
|
||||
const sessions = readSessions();
|
||||
const nowMs = Date.now();
|
||||
for (const session of sessions) {
|
||||
if (session.accountId === user.id) {
|
||||
session.rotatesAtMs = nowMs;
|
||||
broadcastEvent({
|
||||
id: await nextEventId(),
|
||||
id: nextEventId(),
|
||||
type: "session-expired",
|
||||
sessionId: session.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
await writeSessions(sessions);
|
||||
writeSessions(sessions);
|
||||
}
|
||||
|
||||
// Update Schedule counts.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue