Decoding the "Closed without sending a request" Phalcon Warning
When running phalcon serve in VSCode on Windows, you might encounter a message that looks alarming but is often benign: PHP/Phalcon Error: Closed without sending a request; it was probably just an unused speculative preconnection. This specific error usually appears alongside code that seems to be working fine, like the controller action below where logs stop printing after a database save.
public function createAction() {
$this->view->disable();
$formData = $this->request->getJsonRawBody();
$user = new Users();
$user->first_name = $formData->first_name;
$user->last_name = $formData->last_name;
$user->email_address = $formData->email_address;
$user->password = $formData->password;
// this prints to my debug log.
$result = $user->save();
AppLogger::$logger->debug(print_r($result, true)); // this does not print.
AppLogger::$logger->debug("oh boy #2"); // this does not print either.
echo Json::encode($result);
}
In the snippet above, notice that AppLogger::$logger->debug() calls cease outputting to your console immediately after $user->save(). While the error message suggests a connection issueâspecifically regarding an "unused speculative preconnection"âthe root cause in this context is almost certainly how Phalcon handles response streaming versus standard PHP buffering.
The Streaming vs. Buffering Conflict
The error message references "sending a request" and "preconnections," which are concepts borrowed from HTTP/2 server push or connection pooling. In the context of phalcon serve, these warnings often trigger when the internal output buffer is flushed prematurely or when the framework attempts to send headers before all data is ready, only for the underlying stream (the PHP process) to close unexpectedly.
However, the immediate symptom here is the missing log output. When Phalcon detects that it cannot write further to the standard output stream because the response body has already been started (via echo or implicit buffering), subsequent calls to logger->debug() often fail silently or throw these specific connection errors.
The line $result = $user->save(); likely performs a database operation that triggers a flush of internal buffers. Once the buffer is flushed, any attempt to write more text (like your debug logs) encounters a closed stream. The warning about the "speculative preconnection" is essentially Phalcon complaining that it tried to maintain a connection state for logging or output buffering that was already severed by the previous operation.
Resolving the Output Issue
To fix the missing logs and silence the error, you must ensure that your response is fully constructed before attempting any additional logging, or you must explicitly handle the output buffering.
First, verify that your database save method does not implicitly flush headers. If $user->save() returns a result object that triggers internal PHP callbacks, it might be closing the stream. A safer approach is to capture the output buffer status or ensure the response is sent cleanly.
You can modify your controller action to check if the response has already been sent before attempting to log:
public function createAction() {
$this->view->disable();
$formData = $this->request->getJsonRawBody();
$user = new Users();
// Populate user properties
$user->first_name = $formData->first_name;
$user->last_name = $formData->last_name;
$user->email_address = $formData->email_address;
$user->password = $formData->password;
$result = $user->save();
// Check if headers have been sent or output buffering is active
if (!headers_sent() && !ob_get_level()) {
AppLogger::$logger->debug(print_r($result, true));
AppLogger::$logger->debug("oh boy #2");
} else {
AppLogger::$logger->error("Output already sent or buffer full. Logs suppressed.");
}
echo Json::encode($result);
}
Additionally, consider disabling output buffering for the entire request if you are mixing JSON responses with internal logging. This can be done by calling ob_implicit_flush(false) at the start of your application bootstrap or within the controller, provided it doesn't conflict with other dependencies.
Environment-Specific Nuances
Since you are running phalcon serve in VSCode on Windows, keep in mind that the development server uses a single PHP process for all requests. Unlike production setups where Nginx or Apache handle connections separately, errors related to "closed connections" in this environment often stem from how the IDE's debugger interacts with the output stream or how the built-in server manages its internal pipe.
If you find these warnings persist despite fixing the logic, try running phalcon serve directly from a terminal window instead of relying solely on VSCode's integrated terminal. Sometimes the redirection of stdout in the IDE interferes with Phalcon's ability to report these specific stream errors accurately.
Ultimately, this error is a symptom of trying to write to a stream that has already been utilized for the response body. By ensuring your logging happens before any data is echoed or by managing buffer levels explicitly, you can eliminate both the confusing warning and the silent log failures.