Double Response (ERR_HTTP_HEADERS_SENT) in addProjectToCollection controller
p5.js version
1.11.11
What is your operating system?
Mac OS
Web browser and version
Safari 17.6
Actual Behavior
The addProjectToCollection controller (found in server/controllers/collection.controller.js) contains a logic flaw in its Promise handling that leads to a server crash (ERR_HTTP_HEADERS_SENT).
When a validation error occurs (e.g. a collection or a project is not found), the updateCollection function correctly sends a failure response to the client. However, it then returns null to the next block in the Promise chain rather than "short-circuiting" the execution.
addProjectToCollection.js :
First response sent to client here. Returning 'null' doesn't stop the Promise chain. It passes 'null' to the next .then() block.
function updateCollection([collection, project]) {
if (collection == null) {
sendFailure(404, 'Collection not found');
return null;
}
Then, even if updateCollection returned null, .then(populateReferences) runs. Second response occurs when .then(sendSuccess), attempts to send 200OK and crash.
return Promise.all([collectionPromise, projectPromise])
.then(updateCollection)
.then(populateReferences)
.then(sendSuccess)
.catch(sendFailure);
Error in Terminal:
node:_http_outgoing:655 throw new ERR_HTTP_HEADERS_SENT('set'); ^
Error: Cannot set headers after they are sent to the client
Expected Behavior
When a resource (Collection or Project) is not found, the server should respond with a 404 Not Found and terminate the request lifecycle immediately.
The Promise chain should be short-circuited so that subsequent .then() blocks (like populateReferences and sendSuccess) are never executed after a failure response has been sent. The Node process should remain operational, and no ERR_HTTP_HEADERS_SENT error should be triggered.
Steps to reproduce
Steps:
This bug is triggered by a race condition where the Frontend UI becomes "stale" due to actions in another session/tab. For example:
- Ensure user is logged in
- Open the p5.js Web Editor in two separate browser tabs (Tab A and Tab B).
- In Tab A: Open the "Add to Collection" modal for any sketch. The list of existing collections is displayed, if none, create one.
- In Tab B, delete the collection that you intend to add the sketch to.
- Back in Tab, click the button to add the sketch to that (now deleted) collection.
Observed Result: The server identifies the collection is missing, sends a 404, but then fails to stop execution, attempts a second response, and crashes the backend process.
Also happens when: user has a sketch open in Tab A, deletes it in Tab B, and then attempts to add the now deleted sketch from Tab A to a collection.
https://github.com/user-attachments/assets/f27acf43-2d59-4b4c-b5a5-2eb618369755
If approved for work, I'd be happy to take this on.
Proposed solution aiming to keep the current style: the Promise chain must be explicitly short-circuited by throwing an error or checking for a null state in every subsequent .then() block. Otherwise, an async/await approach would require refactoring.
Thanks for catching this @dpoppe7, I think the proposed solution makes sense to me—assigned!
hello @dpoppe7 are you still working on this? if not, I will be happy to work on the fix
hey @raclim, I think this issue is abandoned, can I take up this issue? would love to work on it!
hey @limzykenneth , I think this issue is abandoned, can I take up this issue? would love to work on it!
Hi @raclim — I reproduced this crash locally on develop and confirmed the root cause described above: updateCollection sends a failure response and then return null, but that doesn't stop the promise chain, so .then(populateReferences) runs on null, rejects, and .catch(sendFailure) sends a second response → ERR_HTTP_HEADERS_SENT → the whole server process crashes.
While confirming it, I found that the sibling controller removeProjectFromCollection.js (DELETE /collections/:id/:projectId) has the identical bug from the same root cause. It isn't mentioned in this issue:
Repro (logged in):
DELETE /editor/collections/<nonexistent-id>/<anything>
The API returns 404 {"success":false,"message":"Collection not found"} and then the server crashes with ERR_HTTP_HEADERS_SENT at removeProjectFromCollection.js:8. The same happens for a collection owned by another user (the 403 path). I checked the other two: createCollection and updateCollection are not affected.
This issue has been assigned for a while without a linked PR — if the current assignees are no longer working on it, I'd be glad to take it on and fix both addProjectToCollection and removeProjectFromCollection together, since they share one root cause. My proposed approach is to have the validation paths throw an error carrying a status code so the single .catch(sendFailure) handles them exactly once, instead of sending early and continuing the chain. I've tested this locally and the endpoints now return the correct 4xx without crashing. Happy to add unit tests for the controllers as part of the PR.