Page MenuHomePhabricator

Formalize and centralize signal handling in libphutil scripts
ClosedPublic

Authored by epriestley on Sep 7 2016, 1:01 AM.
Tags
None
Referenced Files
F14076420: D16504.id39718.diff
Thu, Nov 21, 5:15 PM
Unknown Object (File)
Tue, Nov 19, 2:02 PM
Unknown Object (File)
Mon, Nov 18, 5:10 AM
Unknown Object (File)
Thu, Nov 14, 11:45 PM
Unknown Object (File)
Mon, Nov 11, 6:26 AM
Unknown Object (File)
Thu, Nov 7, 3:51 AM
Unknown Object (File)
Fri, Nov 1, 9:47 AM
Unknown Object (File)
Sat, Oct 26, 11:14 AM
Subscribers

Details

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.

Diff Detail

Repository
rPHU libphutil
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

epriestley retitled this revision from to Formalize and centralize signal handling in libphutil scripts.
epriestley updated this object.
epriestley edited the test plan for this revision. (Show Details)
epriestley added a reviewer: chad.

Note that this doesn't actually solve the issue in T11592, it just makes us respond to SIGTERM better. We still send SIGKILL instead of SIGTERM. The next change will switch us to SIGTERM + wait + SIGKILL.

chad edited edge metadata.
This revision is now accepted and ready to land.Sep 7 2016, 1:14 AM
This revision was automatically updated to reflect the committed changes.

Sorry if this is the wrong place for this, but do note that this breaks libphutil (and therefore arcanist) on Windows, since Process Control support and pcntl_signal() are not available on Windows according to http://www.php.net/manual/en/intro.pcntl.php.

sp@sp-lt2 MINGW64 /c/libphutil (master)
$ arc
[2016-09-07 06:31:38] EXCEPTION: (Error) Call to undefined function pcntl_signal() at [<phutil>\src\future\exec\PhutilSignalRouter.php:16]
phutil(head=master, ref.master=e94f6e738e2d)
#0 PhutilSignalRouter::initialize() called at [<phutil>\scripts\__init_script__.php:85]
#1 __phutil_init_script__() called at [<phutil>\scripts\__init_script__.php:91]
#2 include_once(string) called at [C:\arcanist\scripts\__init_script__.php:45]
#3 require_once(string) called at [C:\arcanist\scripts\arcanist.php:6]