Orphaned attachments rows on failed upload
Is there an existing issue for this?
- I have searched the existing issues
This is not related to configuring Outline
- The issue is not related to self-hosting config
Current Behavior
A failed file upload still leaves an attachments row in the database with no matching object in
storage. The user sees an "Upload failed" message, but the row stays behind forever.
The row is created in attachments.create before a single byte has been uploaded
(server/routes/api/attachments/attachments.ts:134). The client receives the attachment plus the
upload details, then pushes the file in a second step via XMLHttpRequest
(app/utils/files.ts:68-140). Nothing ties the two steps together: no status column, no TTL, no
reconciliation job. There is no branch anywhere that deletes the row when step two fails —
grep -rn "rollback" over plugins/storage/server/api/files.ts returns nothing.
Every failure path leaks a row:
| # | Failure path | Location | Row deleted? |
|---|---|---|---|
| 1 | Client never sends step two (tab closed, network lost, cancelled) | app/utils/files.ts:68-140 |
No |
| 2 | XHR network error / status outside 2xx-3xx | app/utils/files.ts:142-144 |
No |
| 3 | files.create missing the file parameter |
plugins/storage/server/api/files.ts:48-50 |
No |
| 4 | files.create bad signature or missing session |
plugins/storage/server/api/files.ts:52-57 |
No |
| 5 | files.create key belongs to another user → AuthorizationError |
plugins/storage/server/api/files.ts:64-67 |
No |
| 6 | ValidationError because file.size > declaredSize |
plugins/storage/server/api/files.ts:69-77 |
No |
| 7 | writeFile throws (permission, timeout, provider error) |
plugins/storage/server/api/files.ts:79-88 |
No |
| 8 | Presigned PUT/POST to S3 fails — the server never finds out | server/routes/api/attachments/attachments.ts:146-199 |
No |
| 9 | attachments.createFromUrl → UploadAttachmentFromUrlTask fails |
server/queues/tasks/UploadAttachmentFromUrlTask.ts:42-44 |
No |
In path 9 the task only does return { error } once attempts is exhausted; the row created at
server/routes/api/attachments/attachments.ts:233-251 with size: 0 stays behind. In paths 1 and 2
the client never reports back to the server: uploadFile just does
throw new Error("Upload failed") (app/utils/files.ts:142-144).
No job cleans them up either. CleanupExpiredAttachmentsTask scans by expiresAt < now()
(server/queues/tasks/CleanupExpiredAttachmentsTask.ts:11-18), and expiresAt is only set for the
Import / WorkspaceImport presets (server/models/helpers/AttachmentHelper.ts:90-98); a normal
attachment gets undefined → a null column (server/models/Attachment.ts:69-70) → it never matches
the cron's where clause. No column distinguishes a row that has received bytes from a freshly
created row still waiting for its upload.
Side effect: Attachment.getTotalSizeForTeam does SUM(size) over every row, orphans included
(server/models/Attachment.ts:214-231), so the team's reported storage usage is inflated.
Where the bug lives:
| # | Location | What is there |
|---|---|---|
| 1 | server/routes/api/attachments/attachments.ts:134-145 |
Creates the row before any byte arrives; no "not uploaded" marker |
| 2 | server/routes/api/attachments/attachments.ts:233-251 |
createFromUrl creates a size: 0 row before queueing the task |
| 3 | server/models/helpers/AttachmentHelper.ts:90-98 |
presetToExpiry only returns a date for Import/WorkspaceImport |
| 4 | server/queues/tasks/CleanupExpiredAttachmentsTask.ts:11-18 |
The cron filters on expiresAt only |
| 5 | plugins/storage/server/api/files.ts:48-77 |
Four throwing branches that exit without cleaning up the row |
| 6 | plugins/storage/server/api/files.ts:79-88 |
The catch around writeFile only rewrites the message, then throw err |
| 7 | server/queues/tasks/UploadAttachmentFromUrlTask.ts:42-44 |
Swallows the upload error, leaves the row behind |
| 8 | app/utils/files.ts:54-144 |
Two-step upload; a failure only throws on the client |
| 9 | server/models/Attachment.ts:214-231 |
getTotalSizeForTeam SUM(size) over every row, orphans included |
Expected Behavior
- When step two of an upload never succeeds, the
attachmentsrow created in step one does not survive: either it is deleted on the failing path, or it is marked as pending and reaped later. - Every throwing branch in
plugins/storage/server/api/files.tscleans up the row it invalidates before rethrowing. UploadAttachmentFromUrlTaskdeletes its row when the upload is abandoned after the final attempt, instead of only returning{ error }.- Rows the client abandons (closed tab, lost network, cancelled upload) are reclaimed by a job, so no row can outlive its missing storage object indefinitely.
Attachment.getTotalSizeForTeamonly counts rows whose bytes actually exist in storage, so team storage usage matches what is stored.
Steps To Reproduce
- Open a document in the editor.
- Start uploading an image or file attachment.
- Force step two to fail — pick any of these:
- Kill the network (or close the tab) right after
attachments.createreturns, before theXMLHttpRequestfinishes. - Make
files.createreject: tamper with the signature, or send a file larger than the declaredsizeso theValidationErrorfires. - Make the storage write fail: revoke write permission on the local upload directory, or point
S3Storageat a bucket the credentials cannot write to.
- Kill the network (or close the tab) right after
- Observe the "Upload failed" message in the UI.
- Query the database:
SELECT id, key, size FROM attachments ORDER BY "createdAt" DESC LIMIT 5;— the row for the failed upload is still there. - Check storage for that row's
key— no object exists. - Wait for
CleanupExpiredAttachmentsTaskto run — the row is still not removed, because itsexpiresAtis null. - Open the workspace storage usage — the failed upload's
sizeis still counted in the total.
For the createFromUrl variant: call attachments.createFromUrl with a URL that cannot be fetched
and let UploadAttachmentFromUrlTask exhaust its attempts — a size: 0 row remains.
Environment
- Outline: 1.10.1 (self-hosted)
- Browser: any — the leak is server-side; reproduced in Chrome and Safari
- Storage provider: any — `LocalStorage` and `S3Storage`, in both `post` and `put` upload modes
- Database: PostgreSQLAnything else?
No response
Source: outline/outline