Timeout-lib, which interrupts everything, also systemcalls. It uses libc-alarm.
Find a file
2013-03-14 16:26:43 +01:00
lib timeout(0) -> never timeout (compatible to Timeout) 2013-03-14 16:26:07 +01:00
test timeout(0) -> never timeout (compatible to Timeout) 2013-03-14 16:26:07 +01:00
.document Initial commit to ruby-timeout-interrupt. 2013-03-06 10:18:40 +01:00
.gitignore license, tests, lib. 2013-03-06 11:58:33 +01:00
Gemfile :doc:. alarm_trap will call raise_if_sb_timed_out and setup. raise_if_sb_timed_out is the old alarm_trap (but alarm_trap should be used like before). setup recognize time outs lesser than one second now. 2013-03-07 14:47:48 +01:00
Gemfile.lock :doc:. alarm_trap will call raise_if_sb_timed_out and setup. raise_if_sb_timed_out is the old alarm_trap (but alarm_trap should be used like before). setup recognize time outs lesser than one second now. 2013-03-07 14:47:48 +01:00
LICENSE.txt Version bump to 0.1.1 2013-03-06 15:09:39 +01:00
Rakefile License changed (LICENSE.txt was LGPLv3, but Gem not) 2013-03-07 14:50:05 +01:00
README.md Classes 2013-03-07 12:30:07 +01:00
timeout-interrupt.gemspec Regenerate gemspec for version 0.3.0 2013-03-14 16:26:43 +01:00
VERSION Version bump to 0.3.0 2013-03-14 16:26:28 +01:00

timeout-interrupt

Works like ruby's timeout, but interrupts every call, also syscalls, which blocks the hole ruby-process.

It uses POSIX's alarm and traps ALRM-signals.

Known limitations bacause of alarm and ALRM are, that you can not use alarm or trap ALRM.

Scopes

If you need scopes with inner and outer time outs, you should know:

The first timed out Timeout will be raised:

include TimeoutInterrupt
timeout(1) { # Will be raised
	timeout(10) { sleep 2 } # Will not be raised
}

If you want to know, which was raised, you need custom exceptions:

class CustomErrorWillBeRaised <Exception
end
class CustomErrorNotRaise <Exception
end
include TimeoutInterrupt
timeout( 1, CustomErrorWillBeRaised) { # Will be raised again
	timeout( 10, CustomErrorNotRaise) { sleep 2 } # Will not be raised
}

Problems

Memory-Leaks or no clean up

Do not forget, syscall can have allocated memory. If you interrupt a call, which can not free his allocations, you will have a memory leak. If it opens a file, reads it and closes it and while it reads, a time out occurs, the file will not be closed.

So, use it only, if your process did not live any longer or if you call something, which never allocate mem or opens a file.

Every time, a process dies, all his memory will be freed and every file will be closed, so let your process die and you should be safe.

Exception-handling

Timeouts can break your exception-handling! You should not handling exception while you wait for a timeout:

include TimeoutInterrupt
timeout(1) {
	begin
		transaction_begin
		do_something
	ensure
		clean_up
		transaction_end
	end
}

Same happens, if clean_up will raise an exception.

And same problem you have with ruby's Timeout.timeout.

Copyleft

Copyright (c) 2013 Denis Knauf. See LICENSE.txt for further details.