Timeout not working while regular expression match is running
Experimenting with dozens of regular expressions that take a long time to match against certain strings, I have found that for some regular expression matches, Timeouts don't work at all (while they work for the rest without problems).
The attached file allows reproduction. On three different machines and with six different versions of Ruby (see below), the attached file always showed about 20~25 seconds of execution time even though there was a Timeout interval of only 10s. If your machine is too fast so that the printed-out result comes in below or just a bit above 10s, then either lower the timeout duration (e.g. to 5s) or increase the length of the string (e.g. 80_000 -> 100_000; caution, time increases with the second power of this number).
The timeout uses
rescue Timeout::Error as described in the pickaxe.
Checked with the following versions/environments:
- ruby 3.1.0dev (2021-06-03T06:59:33Z master 7e14762159) [x86_64-linux] on Ubuntu on WSL on a Windows 10 notebook
- ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-cygwin] on same notebook
- ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x64-mingw32] on same notebook
- ruby 3.1.0dev (2021-06-03T06:59:33Z master 7e14762159) [x86_64-linux] on Ubuntu on WSL on same notebook
- ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu] on a two-CPU Dell server with Ubuntu
- ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux] on an older Dell server with CentOS
Updated by jeremyevans0 (Jeremy Evans) 3 months ago
- Status changed from Open to Closed
This issue appears fixed in Ruby 3.2, due to regexp optimizations by @makenowjust. If there are regexps that are still experiencing this issue, please report them and we can reopen.