fix: prevent import hanging by adding abort signals and idle timeouts
CI / check (push) Successful in 28s
CI / release (push) Skipped
CI / deploy (push) Successful in 59s

- Furni batch: wire request.signal to abort controller, send post_import
  progress events, skip post-import steps when aborted
- Clone sync-all: pre-fetch furnidata once per source instead of per item
  (eliminates 2000+ redundant DB reads + HTTP requests)
- SSE client: add 60s idle timeout to prevent infinite hangs when server
  stops responding
This commit is contained in:
openhands committed 2026-08-31 18:11:54 +02:00
1 parent 96c33efced
commit f70d96b81c
3 files changed
+179 -85

No files matched your search

+125 -65
View File
@@ -67,9 +67,19 @@ export const POST = withAdmin(
await ensureDirectories();
const encoder = new TextEncoder();
// Wire client disconnect to abort controller so we stop processing
// when the user navigates away or closes the browser.
const abortController = new AbortController();
let aborted = false;
request.signal.addEventListener("abort", () => {
aborted = true;
abortController.abort();
});
const stream = new ReadableStream({
async start(controller) {
const send = (data: unknown) => {
if (aborted) return;
try {
controller.enqueue(
encoder.encode(`data: ${JSON.stringify(data)}\n\n`),
@@ -92,6 +102,8 @@ export const POST = withAdmin(
// Process in chunks of `concurrency`
for (let i = 0; i < items.length; i += concurrency) {
if (aborted) break;
const chunk = items.slice(i, i + concurrency);
const promises = chunk.map(async (item, chunkIdx) => {
@@ -182,6 +194,22 @@ export const POST = withAdmin(
await new Promise((r) => setImmediate(r));
}
if (aborted) {
send({
type: "batch_complete",
succeeded,
failed,
warnings: withWarnings,
aborted: true,
duration: Date.now() - startTime,
});
controller.close();
return;
}
// Post-import steps: send progress so the user knows what's happening
send({ type: "post_import", status: "Writing FurnitureData.json..." });
// Batch write FurnitureData.json once at the end — BEFORE RCON so the
// emulator never sees items that the Nitro client can't render.
let furniDataWriteOk = true;
@@ -201,8 +229,11 @@ export const POST = withAdmin(
}
// Refresh game server caches once — skip if FurnitureData write failed
// to avoid an inconsistent state (emulator sees items, client doesn't).
if (furniDataWriteOk) {
if (furniDataWriteOk && !aborted) {
send({
type: "post_import",
status: "Refreshing emulator caches...",
});
try {
const okCat = await rcon.updateCatalog();
const okItems = await rcon.updateItems();
@@ -223,77 +254,106 @@ export const POST = withAdmin(
}
}
// Force offer_id to its row id and fix asset ownership across the tree
let offerIdsFixed = 0;
let ownershipFixed: string[] = [];
let nitrosSynced: string[] = [];
let iconsSynced: string[] = [];
let furniDataFixedIds = 0;
let furniDataFixedOfferIds = 0;
let furniDataMissing = 0;
let furniDataConflicts = 0;
let offerRebuildFixed = 0;
let spriteFixed = 0;
let interactionFixed = 0;
let languages: Awaited<
ReturnType<typeof buildLocalizedFurniDataFiles>
> = [];
try {
offerIdsFixed = (await reconcileImportedOfferIds()).fixed;
ownershipFixed = (await ensureFurniOwnership()).fixed;
const assetsSynced = await syncAssetsToGamedataBundle();
nitrosSynced = assetsSynced.copiedNitros;
iconsSynced = assetsSynced.copiedIcons;
const furniReconcile = await reconcileFurniDataWithItemsBase();
furniDataFixedIds = furniReconcile.fixedIds;
furniDataFixedOfferIds = furniReconcile.fixedOfferIds;
furniDataMissing = furniReconcile.missing;
furniDataConflicts = furniReconcile.conflicts;
offerRebuildFixed = (await rebuildCatalogOfferIds()).fixed;
spriteFixed = (await verifyAndFixSpriteIds()).fixed;
interactionFixed = (await verifyAndFixInteractionModesCount()).fixed;
if (!aborted) {
send({
type: "post_import",
status: "Reconciling offer IDs & ownership...",
});
// Post-import reconciliation — wrapped in try/catch so one
// failure doesn't block the rest.
let offerIdsFixed = 0;
let ownershipFixed: string[] = [];
let nitrosSynced: string[] = [];
let iconsSynced: string[] = [];
let furniDataFixedIds = 0;
let furniDataFixedOfferIds = 0;
let furniDataMissing = 0;
let furniDataConflicts = 0;
let offerRebuildFixed = 0;
let spriteFixed = 0;
let interactionFixed = 0;
let languages: Awaited<
ReturnType<typeof buildLocalizedFurniDataFiles>
> = [];
try {
languages = await buildLocalizedFurniDataFiles();
offerIdsFixed = (await reconcileImportedOfferIds()).fixed;
ownershipFixed = (await ensureFurniOwnership()).fixed;
const assetsSynced = await syncAssetsToGamedataBundle();
nitrosSynced = assetsSynced.copiedNitros;
iconsSynced = assetsSynced.copiedIcons;
const furniReconcile = await reconcileFurniDataWithItemsBase();
furniDataFixedIds = furniReconcile.fixedIds;
furniDataFixedOfferIds = furniReconcile.fixedOfferIds;
furniDataMissing = furniReconcile.missing;
furniDataConflicts = furniReconcile.conflicts;
offerRebuildFixed = (await rebuildCatalogOfferIds()).fixed;
spriteFixed = (await verifyAndFixSpriteIds()).fixed;
if (!aborted) {
send({
type: "post_import",
status: "Verifying interaction modes...",
});
interactionFixed = (await verifyAndFixInteractionModesCount())
.fixed;
}
try {
languages = await buildLocalizedFurniDataFiles();
} catch (err) {
console.warn(
"[import-furni] Localized furnidata build failed:",
(err as Error).message,
);
}
} catch (err) {
console.warn(
"[import-furni] Localized furnidata build failed:",
"[import-furni] Post-import reconcile/ownership failed:",
(err as Error).message,
);
}
} catch (err) {
console.warn(
"[import-furni] Post-import reconcile/ownership failed:",
(err as Error).message,
);
send({
type: "post_import",
status: "Fixing database consistency...",
});
const { catalogNameFixed, haveOfferFixed, costCreditsFixed } =
await fixDatabaseConsistencyAfterImport();
send({
type: "batch_complete",
succeeded,
failed,
warnings: withWarnings,
offerIdsFixed,
ownershipFixed,
nitrosSynced,
iconsSynced,
furniDataFixedIds,
furniDataFixedOfferIds,
furniDataMissing,
furniDataConflicts,
offerRebuildFixed,
spriteFixed,
interactionFixed,
catalogNameFixed,
haveOfferFixed,
costCreditsFixed,
languages,
duration: Date.now() - startTime,
});
} else {
send({
type: "batch_complete",
succeeded,
failed,
warnings: withWarnings,
aborted: true,
duration: Date.now() - startTime,
});
}
// Fix database consistency (catalog_name + have_offer + cost_credits) after import
const { catalogNameFixed, haveOfferFixed, costCreditsFixed } =
await fixDatabaseConsistencyAfterImport();
send({
type: "batch_complete",
succeeded,
failed,
warnings: withWarnings,
offerIdsFixed,
ownershipFixed,
nitrosSynced,
iconsSynced,
furniDataFixedIds,
furniDataFixedOfferIds,
furniDataMissing,
furniDataConflicts,
offerRebuildFixed,
spriteFixed,
interactionFixed,
catalogNameFixed,
haveOfferFixed,
costCreditsFixed,
languages,
duration: Date.now() - startTime,
});
controller.close();
},
});