Skip to content

Commit a625d05

Browse files
committed
fix: enforce timeout
1 parent e6a193e commit a625d05

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

lib/knapsack_pro/repository_adapters/git_adapter.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,34 @@ def git_commit_authors
4848
end
4949

5050
def git_unshallow
51-
command = 'git fetch --shallow-since "one month ago" --quiet 2>/dev/null'
52-
Timeout.timeout(5) { `#{command}` }
53-
rescue Timeout::Error
54-
KnapsackPro.logger.debug("Skip the `#{command}` command because it took too long.")
51+
args = ['git', 'fetch', '--quiet', '--shallow-since', 'one month ago']
52+
53+
begin
54+
pid = Process.spawn(*args, err: File::NULL, pgroup: true)
55+
rescue StandardError => e
56+
KnapsackPro.logger.debug("Failed to unshallow (#{args.join(' ')}): #{e.message}")
57+
return
58+
end
59+
60+
begin
61+
Timeout.timeout(5) { safe_waitpid(pid) }
62+
rescue Timeout::Error
63+
safe_kill(-pid)
64+
safe_waitpid(pid)
65+
KnapsackPro.logger.debug("Failed to unshallow (#{args.join(' ')}) in 5 seconds")
66+
end
67+
end
68+
69+
def safe_waitpid(pid)
70+
Process.waitpid(pid)
71+
rescue Errno::ECHILD
72+
nil
73+
end
74+
75+
def safe_kill(pid)
76+
Process.kill('KILL', pid)
77+
rescue Errno::ESRCH
78+
nil
5579
end
5680

5781
def git_build_author

0 commit comments

Comments
 (0)