Skip to content

fix: server crash on /theme.css request (ERR_STREAM_WRITE_AFTER_END)#39408

Open
sahillllllllll-bit wants to merge 1 commit into
RocketChat:developfrom
sahillllllllll-bit:fix-theme-css-write-after-end
Open

fix: server crash on /theme.css request (ERR_STREAM_WRITE_AFTER_END)#39408
sahillllllllll-bit wants to merge 1 commit into
RocketChat:developfrom
sahillllllllll-bit:fix-theme-css-write-after-end

Conversation

@sahillllllllll-bit
Copy link
Copy Markdown
Contributor

@sahillllllllll-bit sahillllllllll-bit commented Mar 6, 2026

Description

Fixes #39407

Requesting /theme.css may crash the server with:


Error [ERR_STREAM_WRITE_AFTER_END]: write after end

This happens because the middleware ends the response using res.end() but does not stop execution afterward. In some cases the request stream continues and attempts to write to the same response again, causing Node.js to throw ERR_STREAM_WRITE_AFTER_END.

This change ensures the middleware stops execution immediately after sending the response.

Additionally, the Content-Length header now uses Buffer.byteLength instead of string.length to correctly handle UTF-8 characters.

Changes

  • Stop middleware execution after res.end()
  • Use Buffer.byteLength(style, 'utf8') for accurate Content-Length

Summary by CodeRabbit

  • Bug Fixes
    • Fixed Content-Length header calculation for CSS file delivery. The header now accurately reflects the actual byte length of CSS content, ensuring reliable content delivery and preventing potential issues with stylesheet serving.

@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot Bot commented Mar 6, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 6, 2026

⚠️ No Changeset found

Latest commit: 4be8933

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 6, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0dcb7dd8-cf40-4f65-af56-97b5ed068804

📥 Commits

Reviewing files that changed from the base of the PR and between 46a1774 and 4be8933.

📒 Files selected for processing (1)
  • apps/meteor/app/theme/server/server.ts
📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/app/theme/server/server.ts
🧠 Learnings (2)
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/app/theme/server/server.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/app/theme/server/server.ts
🔇 Additional comments (1)
apps/meteor/app/theme/server/server.ts (1)

40-43: Nice fix on the response framing.

Buffer.byteLength(style, 'utf8') gives the correct Content-Length for UTF-8 CSS payloads, so clients won't see a mismatched body size here.


Walkthrough

Modified HTTP response Content-Length calculation in the theme CSS endpoint from using raw string length to using Buffer.byteLength with UTF-8 encoding, ensuring the Content-Length header accurately reflects the actual byte size of the CSS content.

Changes

Cohort / File(s) Summary
Theme CSS Response Handling
apps/meteor/app/theme/server/server.ts
Changed Content-Length calculation from string length to Buffer.byteLength with UTF-8 encoding to correctly represent the actual byte length of CSS content in the HTTP response header.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested labels

type: bug

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and specifically describes the fix: preventing a server crash caused by writing after stream end on /theme.css requests.
Linked Issues check ✅ Passed The PR changes align with issue #39407 requirements: using Buffer.byteLength for correct Content-Length calculation to handle UTF-8 properly and stopping middleware execution to prevent writes after end.
Out of Scope Changes check ✅ Passed All changes in apps/meteor/app/theme/server/server.ts are directly scoped to fixing the /theme.css ERR_STREAM_WRITE_AFTER_END error without unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/theme.css request can crash the server with `ERR_STREAM_WRITE_AFTER_END

1 participant