Skip to content

Commit 9b5060a

Browse files
committed
http-libs: add test about error logging
While this does not exercise the exact error that can happen in long migrations, it gets logged in a similar way. There's no easy way to trigger the issue, the best chance is to send a malformed response to trigger a Parse_error. I did modify the code in http_client and verified that current code can produce the logging, with backtraces successfully, when set up properly (like in the test client) Signed-off-by: Pau Ruiz Safont <[email protected]>
1 parent 805d142 commit 9b5060a

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

ocaml/libs/http-lib/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@
113113
safe-resources
114114
stunnel
115115
threads.posix
116+
xapi-backtrace
117+
xapi-log
116118
xapi-stdext-pervasives
117119
xapi-stdext-unix
118120
)

ocaml/libs/http-lib/test_client.ml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,44 @@ let perf () =
167167
in
168168
Printf.printf "%s RPCs/sec\n%!" (Normal_population.to_string thread_persistent)
169169

170+
let send_close_conn ~use_fastpath ~use_framing keep_alive s =
171+
try
172+
Http_client.rpc ~use_fastpath s
173+
(Http.Request.make ~frame:use_framing ~version:"1.1" ~keep_alive
174+
~user_agent ~body:"hello" Http.Get "/close_conn"
175+
) (fun response s ->
176+
match response.Http.Response.content_length with
177+
| Some l ->
178+
let _ = Unixext.really_read_string s (Int64.to_int l) in
179+
Printf.printf "Received a response with %Ld bytes.\n" l ;
180+
exit 1
181+
| None ->
182+
Printf.printf "Need a content length\n" ;
183+
exit 1
184+
)
185+
with Unix.Unix_error (Unix.ECONNRESET, "read", "") as e ->
186+
Backtrace.is_important e ;
187+
let bt = Backtrace.get e in
188+
Debug.log_backtrace e bt
189+
190+
let ( let@ ) f x = f x
191+
192+
let logerr () =
193+
(* Send a request to the server to close connection instead of replying with
194+
an http request, force the error to be logged *)
195+
Printexc.record_backtrace true ;
196+
Debug.log_to_stdout () ;
197+
Debug.set_level Syslog.Debug ;
198+
let use_fastpath = !use_fastpath in
199+
let use_framing = !use_framing in
200+
let transport = if !use_ssl then with_stunnel else with_connection in
201+
let call () =
202+
let@ () = Backtrace.with_backtraces in
203+
let@ s = transport !ip !port in
204+
send_close_conn ~use_fastpath ~use_framing false s
205+
in
206+
match call () with `Ok () -> () | `Error (_, _) -> ()
207+
170208
let () =
171209
Arg.parse
172210
[
@@ -176,6 +214,7 @@ let () =
176214
; ("-frame", Arg.Set use_framing, "use HTTP framing")
177215
; ("--ssl", Arg.Set use_ssl, "use SSL rather than plaintext")
178216
; ("--perf", Arg.Unit perf, "Collect performance stats")
217+
; ("--logerr", Arg.Unit logerr, "Test log on error")
179218
]
180219
(fun x -> Printf.fprintf stderr "Ignoring unexpected argument: %s\n" x)
181220
"A simple test HTTP client"

ocaml/libs/http-lib/test_client_server.t

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@
44
$ sleep 0.1
55

66
== Normal
7-
$ ./test_client.exe --perf > /dev/null
7+
$ ./test_client.exe --perf > /dev/null
8+
9+
== Expect to log after a closed connection
10+
$ ./test_client.exe --logerr > result
11+
$ grep "ECONNRESET" result -c
12+
1
13+
$ grep "backtrace" result -c
14+
11
15+
$ grep "Called from" result -c
16+
8

ocaml/libs/http-lib/test_server.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ let _ =
8080
)
8181
)
8282
) ;
83+
(* Forces a protocol error by closing the connection without sending a
84+
proper http reponse code *)
85+
Server.add_handler server Http.Get "/close_conn"
86+
(FdIO (fun _ _ _ -> raise End_of_file)) ;
8387
let ip = "0.0.0.0" in
8488
let inet_addr = Unix.inet_addr_of_string ip in
8589
let addr = Unix.ADDR_INET (inet_addr, !port) in

0 commit comments

Comments
 (0)