http://qs321.pair.com?node_id=138554

erroneousBollock has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I am in a group writing yet another webserver in Perl. This server uses Thread.pm (adaptive pre-threading server). This server has embedded perl pages (like a higher level PHP).

There are "Very Good Reasons" (outside the scope of a simple http daemon) for this being a threaded implementation.

My problem is that I wish to eval() the embedded perl code (yes with Safe.pm) so that said eval() will timeout (in say X seconds), printing whatever had been produced thus-far, with an appropriate timeout error message.

I first thought about alarm(), but quickly realised that signals cannot be delivered to specific threads.

I then tried reusing a pre-existing "monitor" thread in the server to kill off individual threads after a timeout period. Unfortunately, as there is no "kill thread" method or easily forseeable alternative, I had to periodically poll the "monitor" thread from each child.... which of course does not help for killing off a child who is performing some non-deterministic perl user-code.

I toyed with async(), and Time::HiRes, but thusfar have not found the "easy" way I seek. (The embedded perl code, and a timer , race to lock a per-thread scalar, the output or error is printed, the losing-thread is cleaned up).

If anyone has any suggestions, I would be supremely grateful. Either concepts or real code is fine.

PS: I am not too concerned about malicious user code crashing the server, as for applications "not audited by the administrator", the server will fork() a whole new threaded server... which will or won't die.