Skip to content

Commit 0debf32

Browse files
authored
Merge pull request #304 from rbrw/pdb-5215-dont-crash-on-resources-during-sighup
(PDB-5215) Don't crash on resources during sighup
2 parents 7af6642 + a640031 commit 0debf32

File tree

10 files changed

+159
-14
lines changed

10 files changed

+159
-14
lines changed

ext/test/custom-exit-behavior

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ set -uexo pipefail
44

55
usage() { echo "Usage: $(basename "$0")"; }
66
misuse() { usage 1>&2; exit 2; }
7-
tk() { lein trampoline run "$@"; }
87

98
test $# -eq 0 || misuse
109

11-
tmpdir="$(mktemp -d "test-top-level-cli-XXXXXX")"
10+
tmpdir="$(mktemp -d "test-custom-exit-behavior-XXXXXX")"
1211
tmpdir="$(cd "$tmpdir" && pwd)"
1312
trap "$(printf 'rm -rf %q' "$tmpdir")" EXIT
1413

1514
rc=0
16-
tk -d -b ext/test/lib/custom-exit-behavior/bootstrap.cfg \
17-
1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
15+
./tk -cp "$(pwd)/test" -- \
16+
-d -b <(echo puppetlabs.trapperkeeper.custom-exit-behavior-test/custom-exit-behavior-test-service) \
17+
1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
1818
cat "$tmpdir/out" "$tmpdir/err"
1919
test "$rc" -eq 7
2020
grep -F 'Some excitement!' "$tmpdir/out"

ext/test/lib/custom-exit-behavior/bootstrap.cfg

Lines changed: 0 additions & 1 deletion
This file was deleted.

ext/test/run-all

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ test $# -eq 0 || misuse
99

1010
ext/test/top-level-cli
1111
ext/test/custom-exit-behavior
12+
ext/test/signal-handling

ext/test/signal-handling

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
set -uexo pipefail
4+
5+
usage() { echo "Usage: $(basename "$0")"; }
6+
misuse() { usage 1>&2; exit 2; }
7+
8+
await-file()
9+
(
10+
local target="$1"
11+
set +x
12+
while ! test -e "$target"; do sleep 0.1; done
13+
)
14+
15+
tk_pid=''
16+
tmpdir=''
17+
on-exit()
18+
{
19+
if test "$tk_pid"; then
20+
kill "$tk_pid"
21+
status=0
22+
wait "$tk_pid" || status=$?
23+
set +x
24+
echo tk exited with status "$status (143 is likely)" 1>&2
25+
set -x
26+
fi
27+
rm -rf "$tmpdir"
28+
}
29+
trap on-exit EXIT
30+
31+
test $# -eq 0 || misuse
32+
33+
tmpdir="$(mktemp -d "test-signal-handling-XXXXXX")"
34+
tmpdir="$(cd "$tmpdir" && pwd)"
35+
36+
# Start the test server, which repeatedly writes to the configured
37+
# target file, and make sure the target changes after a config file
38+
# change and signal.
39+
40+
target_1="$tmpdir/target-1"
41+
target_2="$tmpdir/target-2"
42+
43+
cat > "$tmpdir/config.json" <<EOS
44+
{"signal-test-target": "$target_1"}
45+
EOS
46+
47+
./tk -cp "$(pwd)/test" -- \
48+
--config "$tmpdir/config.json" \
49+
-b <(echo puppetlabs.trapperkeeper.signal-handling-test/signal-handling-test-service) &
50+
tk_pid=$!
51+
52+
await-file "$target_1"
53+
test exciting = "$(< "$target_1")"
54+
55+
test ! -e "$target_2"
56+
cat > "$tmpdir/config.json" <<EOS
57+
{"signal-test-target": "$target_2"}
58+
EOS
59+
60+
kill -HUP "$tk_pid"
61+
await-file "$target_2"
62+
test exciting = "$(< "$target_2")"

ext/test/top-level-cli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ trap "$(printf 'rm -rf %q' "$tmpdir")" EXIT
1414

1515
## Test handling an unknown option
1616
rc=0
17-
./tk --invalid-option 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
17+
./tk -- --invalid-option 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
1818
cat "$tmpdir/out" "$tmpdir/err"
1919
test "$rc" -eq 1
2020
grep -F 'Unknown option: "--invalid-option"' "$tmpdir/err"
2121

2222

2323
## Test --help
2424
rc=0
25-
./tk --help 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
25+
./tk -- --help 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
2626
cat "$tmpdir/out" "$tmpdir/err"
2727
test "$rc" -eq 0
2828
grep -F 'Path to bootstrap config file' "$tmpdir/out"
@@ -32,7 +32,7 @@ test $(wc -c < "$tmpdir/out") -eq 650
3232

3333
## Test handling a missing bootstrap file
3434
rc=0
35-
./tk frobnicate ... 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
35+
./tk -- frobnicate ... 1>"$tmpdir/out" 2>"$tmpdir/err" || rc=$?
3636
cat "$tmpdir/out" "$tmpdir/err"
3737
test "$rc" -eq 1
3838
grep -F 'Unable to find bootstrap.cfg file via --bootstrap-config' "$tmpdir/err"

src/puppetlabs/trapperkeeper/bootstrap.clj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,18 @@
9393
(log/debug (i18n/trs "Loading bootstrap config from current working directory: ''{0}''" config-file-path))
9494
[config-file-path]))))
9595

96+
(defn- resource [name]
97+
;; Fixes io/resource crashes when context class loader is nil.
98+
;; https://clojure.atlassian.net/browse/CLJ-2431
99+
(.getResource (or (.getContextClassLoader (Thread/currentThread))
100+
(ClassLoader/getSystemClassLoader))
101+
name))
102+
96103
(schema/defn config-from-classpath :- [(schema/maybe schema/Str)]
97104
"Check to see if there is a bootstrap config file available on the classpath;
98105
if so, return it."
99106
[]
100-
(when-let [classpath-config (io/resource bootstrap-config-file-name)]
107+
(when-let [classpath-config (resource bootstrap-config-file-name)]
101108
(log/debug (i18n/trs "Loading bootstrap config from classpath: ''{0}''" classpath-config))
102109
[(str classpath-config)]))
103110

@@ -133,7 +140,7 @@
133140
the config. Throws an exception if there is a problem reading the file or
134141
the URI can't be loaded"
135142
[config-path :- schema/Str]
136-
(if (fs/file? config-path)
143+
(if (fs/readable? config-path)
137144
(line-seq (io/reader (fs/file config-path)))
138145
(try
139146
; If it's not a file, attempt to read it as a URI

src/puppetlabs/trapperkeeper/config.clj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@
5858
(edn/read (PushbackReader. (io/reader file)))
5959

6060
#{".yaml" ".yml"}
61-
(yaml/parse-string (slurp file))))
61+
(yaml/parse-string (slurp file))
62+
63+
(throw (IllegalArgumentException.
64+
(i18n/trs "Config file {0} must end in .conf or other recognized extension"
65+
(-> file str pr-str))))))
6266

6367
(defn override-restart-file-from-cli-data
6468
[config-data cli-data]

src/puppetlabs/trapperkeeper/core.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
(defn main
211211
"Launches the trapperkeeper framework. This function blocks until
212212
trapperkeeper is shut down. This may be called directly, but is also
213-
called by `puppetlabs.trapperkeeper.core/-main` if you use
213+
called by `puppetlabs.trapperkeeper.main/-main` if you use
214214
`puppetlabs.trapperkeeper.core` as the `:main` namespace in your
215215
leinengen project. Never returns (calls System/exit) after argument
216216
processing errors, `--help` requests, or calls to `request-shutdown`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(ns puppetlabs.trapperkeeper.signal-handling-test
2+
(:require
3+
[puppetlabs.trapperkeeper.core :as core]))
4+
5+
(defn- start-test [context get-in-config]
6+
(let [continue? (atom true)
7+
thread (future
8+
(try ;; future just discards top-level exceptions
9+
(while @continue?
10+
(let [target (get-in-config [:signal-test-target])]
11+
(assert target)
12+
(Thread/sleep 200)
13+
(spit target "exciting")))
14+
(catch Throwable ex
15+
(prn ex)
16+
(throw ex))))]
17+
(assoc context
18+
:finish-signal-test
19+
(fn exit-signal-test []
20+
(reset! continue? false)
21+
@thread))))
22+
23+
(defn- stop-test [{:keys [finish-signal-test] :as context}]
24+
(finish-signal-test)
25+
context)
26+
27+
(defprotocol SignalHandlingTestService)
28+
29+
(core/defservice signal-handling-test-service
30+
SignalHandlingTestService
31+
[[:ConfigService get-in-config]]
32+
(init [this context] context)
33+
(start [this context] (start-test context get-in-config))
34+
(stop [this context] (stop-test context)))

tk

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
#!/usr/bin/env bash
22

3-
set -e
3+
set -ueo pipefail
4+
5+
usage() { echo "Usage: tk JVM_ARG ... -- TK_ARG ..."; }
6+
misuse() { usage 1>&2; exit 2; }
47

58
jar_glob='trapperkeeper-*-SNAPSHOT-standalone.jar'
69

10+
# Believe last -cp wins for java, and here, any final -cp path will be
11+
# placed in front of the jar.
12+
13+
cp=''
14+
jvm_args=()
15+
while test $# -gt 0; do
16+
case "$1" in
17+
-h|--help)
18+
usage
19+
exit 0
20+
;;
21+
-cp)
22+
shift
23+
test $# -gt 0 || misuse
24+
cp="$1"
25+
shift
26+
;;
27+
--)
28+
shift
29+
break
30+
;;
31+
*)
32+
shift
33+
jvm_args+=("$1")
34+
;;
35+
esac
36+
done
37+
738
if test "${TRAPPERKEEPER_JAR:-}"; then
839
jar="$TRAPPERKEEPER_JAR"
940
else
@@ -28,4 +59,11 @@ if ! test -e "$jar"; then
2859
exit 2
2960
fi
3061

31-
exec java -jar "$jar" "$@"
62+
set -x
63+
if test "$cp"; then
64+
cp="$cp:$jar"
65+
else
66+
cp="$jar"
67+
fi
68+
69+
exec java -cp "$cp" clojure.main -m puppetlabs.trapperkeeper.main "$@"

0 commit comments

Comments
 (0)