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
|
@ -11,11 +11,11 @@ import { broadcastEvent, cancelAccountStreams } from "~/server/streams";
|
|||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const serverSession = await requireServerSessionWithUser(event);
|
||||
let users = await readUsers();
|
||||
let users = readUsers();
|
||||
|
||||
// Expire sessions for this user
|
||||
const expiredSessionIds = new Set<number>();
|
||||
let sessions = await readSessions();
|
||||
let sessions = readSessions();
|
||||
const nowMs = Date.now();
|
||||
for (const session of sessions) {
|
||||
if (
|
||||
|
@ -25,7 +25,7 @@ export default defineEventHandler(async (event) => {
|
|||
) {
|
||||
session.expiresAtMs = nowMs;
|
||||
broadcastEvent({
|
||||
id: await nextEventId(),
|
||||
id: nextEventId(),
|
||||
type: "session-expired",
|
||||
sessionId: session.id,
|
||||
});
|
||||
|
@ -33,24 +33,24 @@ export default defineEventHandler(async (event) => {
|
|||
}
|
||||
}
|
||||
cancelAccountStreams(serverSession.accountId);
|
||||
await writeSessions(sessions);
|
||||
writeSessions(sessions);
|
||||
await deleteCookie(event, "session");
|
||||
|
||||
// Remove subscriptions for this user
|
||||
let subscriptions = await readSubscriptions();
|
||||
let subscriptions = readSubscriptions();
|
||||
subscriptions = subscriptions.filter(
|
||||
subscription => !expiredSessionIds.has(subscription.sessionId)
|
||||
);
|
||||
await writeSubscriptions(subscriptions);
|
||||
writeSubscriptions(subscriptions);
|
||||
|
||||
// Remove the user
|
||||
const account = users.find(user => user.id === serverSession.accountId)!;
|
||||
const now = new Date(nowMs).toISOString();
|
||||
account.deleted = true;
|
||||
account.updatedAt = now;
|
||||
await writeUsers(users);
|
||||
writeUsers(users);
|
||||
await broadcastEvent({
|
||||
id: await nextEventId(),
|
||||
id: nextEventId(),
|
||||
type: "user-update",
|
||||
data: {
|
||||
id: account.id,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue