Cloudflare “Incoming request ended abruptly: context canceled”

DEFINITIVE TIMEOUT SOLUTION: FIXING ‘CONTEXT CANCELED’ IN CYBERPANEL (LITESPEED/DOCKER)


PROBLEM

The error Incoming request ended abruptly: context canceled (or 503/504 Gateway Timeout) appears in the Docker or LiteSpeed/CyberPanel log, even after increasing the PHP max_execution_time.

This happens because a proxy component or LiteSpeed itself (acting as a proxy for PHP) is closing the connection before the PHP script can finish, disregarding the high limit configured in php.ini.


SOLUTION

The solution consists of ensuring that PHP has the correct execution and termination time and, crucially, that LiteSpeed (in its Virtual Host) is instructed to wait for PHP for the same period.

STEP 1: php.ini Adjustment (Advanced Configuration)

  1. Access CyberPanel: PHP $\rightarrow$ Edit PHP Configs $\rightarrow$ Advanced.

  2. Select your site’s PHP version (e.g., PHP 8.3).

  3. Ensure that the max_execution_time and max_input_time values are set to the desired value (e.g., 300).

  4. Add the crucial directive request_terminate_timeout with a slightly higher value.

Settings to apply in php.ini:

Ini, TOML

; Maximum time a script can run
max_execution_time = 300

; Maximum time for reading the request (input)
max_input_time = 300

; Maximum time PHP-FPM should wait before terminating the process
request_terminate_timeout = 301

  1. Save changes and Restart PHP (PHP $\rightarrow$ Restart PHP).

STEP 2: LiteSpeed Virtual Host Adjustment (SSH)

The context canceled error is usually resolved by instructing LiteSpeed (the frontend) to wait longer for the External Application (PHP).

  1. Access the server via SSH.

  2. Edit your Virtual Host (VH) configuration file, located at:

    Bash

    sudo nano /usr/local/lsws/conf/vhosts/YOUR_DOMAIN/vhconf.conf
    
    

    (Replace YOUR_DOMAIN with the actual name of your Virtual Host.)

  3. Locate the extprocessor section (in our case, it was merca4425) and adjust the timeout directives to a high value (e.g., 600 seconds), ensuring they are greater than the PHP limit (301s).

Settings to apply in vhconf.conf:

Snippet de código

extprocessor merca4425 {
  type                    lsapi
  # ... (other configurations)
  
  # Maximum time to initiate connection with PHP
  initTimeout             600
  
  # Time LiteSpeed waits for a connection retry attempt (CRITICAL)
  retryTimeout            600 
  
  # ... (other configurations)
}

  1. Save the file and Restart OpenLiteSpeed:

    Bash

    sudo /usr/local/lsws/bin/lswsctrl restart
    
    

RESULT

With these synchronized configurations across all layers (PHP and LiteSpeed), the error message Incoming request ended abruptly: context canceled is eliminated, and long-running scripts can be completed successfully within the new defined time limit.