Skip to content

Bug: CLI Commands do not respond to Ctrl-C interrupts (might be an enhancement not a bug) #10589

Description

@daveherman71

PHP Version

8.4

CodeIgniter4 Version

4.7.4

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

cli-server (PHP built-in webserver)

Environment

development

Database

MySQL 8.4.9

What happened?

CLI commands do not respond to the user pressing Ctrl-C to abort

Steps to Reproduce

Create a long running or infinite loop as a command and try and press Ctrl-C. For example:

public function run(array $params)
{
	while ( 1 > 0 ) {
		CLI::write("Waiting...");
	}
}

Expected Output

I expected the execution to halt.

Anything else?

I have (with Claude's assistance) implemented the following workaround which should possibly be included in the core.

public function run(array $params)
{
	// Abort on Ctrl-C

	if (function_exists('sapi_windows_set_ctrl_handler')) {
		// Windows
		sapi_windows_set_ctrl_handler(function ($event) {
			if ($event === PHP_WINDOWS_EVENT_CTRL_C) {
				CLI::error("\nProcess aborted by Ctrl-C.");
				exit(1);
			}
		});
	} else if (function_exists('pcntl_signal')) {
		// Linux / macOS fallback
		pcntl_async_signals(true);
		pcntl_signal(SIGINT, function() {
			CLI::error("\nProcess aborted by Ctrl-C.");
			exit(1);
		});
	}
	...

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions