HomePhabricator

Formalize and centralize signal handling in libphutil scripts

Description

Formalize and centralize signal handling in libphutil scripts

Summary:
Ref T11592. By default, when PHP receives a SIGTERM, it just exits without running destructors.

This makes it difficult for us to release locks, free resources, terminate subprocesses, etc. We already fix this behavior in the daemon wrapper, but I want to extend this to all libphutil scripts so that everything terminates subprocesses correctly. Since pcntl_signal() can only accept one signal handler, that means we need to centralize signal handling.

This creates a centralized signal router and some formal signal handlers, and always installs basic routing behavior for SIGHUP (route, plus dump backtraces) and SIGTERM (exit, but run destructors).

Test Plan:
Used this test script:

<?php

require_once '../libphutil/scripts/__init_script__.php';

echo getmypid()."\n";

$future = new ExecFuture('sleep 240');
$future->resolve();
  • Sent it SIGTERM.
    • Before patch: sleep subprocess still alive.
    • After patch: sleep subprocess killed.
  • Sent it SIGHUP.
    • Verified it dumped a trace properly.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T11592

Differential Revision: https://secure.phabricator.com/D16504