Skip to content

Fix(finalhandler): prevent setting status code after headers sent#6

Open
PlumBlossomMaid wants to merge 1 commit intoTRIGONIM:mainfrom
PlumBlossomMaid:fix/finalhandler-headers-already-sent
Open

Fix(finalhandler): prevent setting status code after headers sent#6
PlumBlossomMaid wants to merge 1 commit intoTRIGONIM:mainfrom
PlumBlossomMaid:fix/finalhandler-headers-already-sent

Conversation

@PlumBlossomMaid
Copy link
Copy Markdown

Problem

When a client disconnects prematurely (e.g., rapid page refresh), finalhandler may attempt to set a status code after response headers have already been sent. This triggers an assertion in pegasus:

can't set status code, it was already sent

Fix #5

Root Cause

finalhandler.lua's send() function does not check whether res.pg_res._headersSended is true before calling res:status().

There is already a check for the 404 case at line 80:

if not err and res.pg_res._headersSended then 
    print("cannot 404 after headers sent") 
    return 
end

However, this does not cover cases where err is present (e.g., 500 errors).

Solution

Added a unified check at the beginning of send():

local send = function(req, res, status, headers, msg)
    if res.pg_res._headersSended then
        return
    end
    -- ... existing logic
end

This extends the protection to all error cases and prevents the pegasus assertion entirely.

Testing

  • Normal requests: unaffected ✅
  • Rapid page refresh: error no longer appears ✅
  • Server stability: unchanged ✅

Environment

  • OS: Windows 10
  • Lua: 5.1.5
  • lua-express: 1.0.0
  • pegasus: 1.1.0

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: "can't set status code, it was already sent" error on premature client disconnect

1 participant