phalcon/cphalcon · error · Phalcon\Http\Response\Exceptions\ResponseAlreadySent

Response was already sent

Error message

Response was already sent

What it means

Response::send() sets an internal sent flag once headers and body have been emitted; calling send() again on the same object throws ResponseAlreadySent to prevent duplicated headers and body output. The companion check is isSent().

Source

Thrown at phalcon/Http/Response.zep:309

    /**
     * Resets all the established headers
     */
    public function resetHeaders() -> <ResponseInterface>
    {
        this->headers->reset();

        return this;
    }

    /**
     * Prints out HTTP response to the client
     */
    public function send() -> <ResponseInterface>
    {
        var content, file;

        if unlikely this->sent {
            throw new ResponseAlreadySent();
        }

        this->sendHeaders();
        this->sendCookies();

        /**
         * Output the response body
         */
        let content = this->content;

        if content != null {
            echo content;
        } else {
            let file = this->file;

            if typeof file == "string" && strlen(file) {
                readfile(file);
            }

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Guard every send: if (!$response->isSent()) { $response->send(); }
  2. Send from exactly one place (front controller) and return Response objects everywhere else
  3. In exception handlers, send and then exit/return so the main loop does not resend

Example fix

// before
$this->response->send();
// ... later, framework front controller also runs:
$this->response->send(); // ResponseAlreadySent

// after
if (!$this->response->isSent()) {
    $this->response->send();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!$response->isSent()) {
    $response->send();
}

Try / catch

try { $response->send(); } catch (\Phalcon\Http\Response\Exceptions\ResponseAlreadySent $e) { // body already delivered: log and continue idempotently
    error_log('Response already sent; skipping duplicate send');
}

Prevention

When it happens

Trigger: $response->send(); $response->send(); an exception handler that sends its own response and then the normal lifecycle sends again; middleware or event listeners calling send() on the shared response object.

Common situations: Error handlers that send and then let the main loop continue; 'after' middleware sending before the front controller does; code migrated from frameworks where double-send was ignored.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/a1490265351f1a36. Report an issue: GitHub.