This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new 8d9f50eb Kill scan-page if it takes too long
8d9f50eb is described below

commit 8d9f50eb132bf0b66357d9648a84d8616b3d9ee3
Author: Sebb <[email protected]>
AuthorDate: Tue May 17 22:41:47 2022 +0100

    Kill scan-page if it takes too long
---
 tools/site-scan.rb | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)

diff --git a/tools/site-scan.rb b/tools/site-scan.rb
index c55d99bb..106b1ae7 100755
--- a/tools/site-scan.rb
+++ b/tools/site-scan.rb
@@ -149,8 +149,8 @@ def parse(id, site, name)
 
   # Check for resource loading from non-ASF domains
   cmd = ['node', '/srv/whimsy/tools/scan-page.js', site]
-  out, err, status = Open3.capture3(*cmd)
-  if status.success?
+  out, err, status = exec_with_timeout(cmd, 30)
+  if status
     ext_urls = out.split("\n").reject {|x| ASFDOMAIN.asfhost? x}.tally
     resources = ext_urls.values.sum
     data[:resources] = "Found #{resources} external resources: #{ext_urls}"
@@ -163,6 +163,43 @@ def parse(id, site, name)
   return data
 end
 
+require 'timeout'
+# the node script appears to stall sometimes, so apply a timeout
+def exec_with_timeout(cmd, timeout)
+  begin
+    # stdout, stderr pipes
+    rout, wout = IO.pipe
+    rerr, werr = IO.pipe
+    stdout, stderr = nil
+    status = false
+
+    pid = Process.spawn(*cmd, pgroup: true, :out => wout, :err => werr)
+
+    Timeout.timeout(timeout) do
+      Process.waitpid(pid)
+      # close write ends so we can read from them
+      wout.close
+      werr.close
+
+      stdout = rout.readlines.join
+      stderr = rerr.readlines.join
+      status = true
+    end
+
+  rescue Timeout::Error
+    stderr = 'Timeout'
+    Process.kill(-9, pid)
+    Process.detach(pid)
+  ensure
+    wout.close unless wout.closed?
+    werr.close unless werr.closed?
+    # dispose the read ends of the pipes
+    rout.close
+    rerr.close
+  end
+  return stdout, stderr, status
+ end
+
 #########################################################################
 # Main execution begins here
 results = {}

Reply via email to