-
-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Address ECONNRESET in issue #118 #165
Open
pm-mck
wants to merge
4
commits into
aantron:master
Choose a base branch
from
pm-mck:#118-handle-ECONNRESET
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93fe295
add a few missing dependencies to dream.opam; allow the dune-project …
pm-mck e6bdf76
allow econnreset to identify an open port
pm-mck 78cf84a
Document Dream.run ?socket_path
aantron 7df24a2
Merge branch 'master' into #118-handle-ECONNRESET
aantron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
(lang dune 2.7) | ||
(cram enable) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
(cram | ||
(applies_to :whole_subtree)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
(executable | ||
(name econnreset) | ||
(libraries dream)) | ||
|
||
(cram | ||
(deps %{exe:econnreset.exe})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
(* This file is part of Dream, released under the MIT license. See LICENSE.md | ||
for details, or visit https://github.com/aantron/dream. | ||
|
||
Copyright 2021 Anton Bachin *) | ||
|
||
let serve port = | ||
print_endline "server mode"; | ||
Dream.run ~greeting:false ~port (fun _ -> Unix.sleepf 10.0; Dream.html "Hello") | ||
|
||
let client port = | ||
print_endline "client mode"; | ||
let open Unix in | ||
let fd = socket PF_INET6 SOCK_STREAM 0 in | ||
(* force the client to send a TCP RST packet if it fails during connection *) | ||
setsockopt_optint fd SO_LINGER (Some 0); | ||
let _ = connect fd (ADDR_INET (inet6_addr_loopback, port)) in | ||
ignore @@ failwith "sending RST" | ||
|
||
let print_open_port () = | ||
let open Unix in | ||
let fd = socket PF_INET6 SOCK_STREAM 0 in | ||
bind fd (ADDR_INET (inet6_addr_loopback, 0)); | ||
|
||
begin match getsockname fd with | ||
| ADDR_INET (_, port) -> Printf.printf "%d\n" port | ||
| _ -> failwith "Invalid Socket response" | ||
end; | ||
|
||
exit 0 | ||
|
||
let () = | ||
let server = ref(false) in | ||
let port = ref(-1) in | ||
let usage = "Test for ECONNRESET errors being reported" in | ||
Arg.parse [ | ||
"-p", Set_int port, "sets the port to listen on or connect to, if not specified, prints an available TCP port and exits"; | ||
"-s", Set server, "enables the server on port [port], if not set sends a TCP RST on [port]" | ||
] (fun _ -> ()) usage; | ||
|
||
let port = !port in | ||
|
||
(* see if we need to print an open port or validate the port *) | ||
if port = -1 then print_open_port () | ||
else if port > 65535 || port < 1025 | ||
then failwith "Port argument (-p) must set and be between 1025-65535"; | ||
|
||
if !server then serve port | ||
else client port | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Start a Dream server | ||
|
||
$ export PORT=$(./econnreset.exe) | ||
$ ./econnreset.exe -s -p "${PORT}" &> test.log & | ||
$ export PID=$! | ||
$ sleep 1 | ||
|
||
Force a connection reset - will log a few errors | ||
|
||
$ ./econnreset.exe -p "${PORT}" | ||
|
||
Does the log contain an error line for the ECONNRESET? An error code of [1] is "good", meaning no line was found. | ||
|
||
$ kill "${PID}" | ||
$ cat test.log | grep 'ERROR' | grep 'ECONNRESET' | ||
|
||
Does the log contain an info line with custom string for the ECONNRESET? | ||
|
||
$ cat test.log | grep 'INFO' | grep 'Connection Reset at Client' | wc -l |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than a cram test, could this be done using an in-process expect test? It seems like that approach would be more portable, as it wouldn't depend on Unix commands, and could be a bit less awkward, since it wouldn't require any complex shell command lines (everything would be kept in OCaml code).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I spent some time today on this, I wanted to report back and see if you have any thoughts on a different approach.
Short answer is that it is possible, but without modifying Dream itself to add test hooks into the http module, quite ugly.
I have a successful expect test which does 3 things:
Dream.serve
with astop
promise.fd
which causes an RST packet to be sent to the server. It then resolves thestop
promise.stop
promise in 4 seconds, the whole thing gets torn down.A few things I don't like:
SIGPIPE
since closing thefd
the way it does signals a closed pipe.Reviewing the expect tests got me thinking: how useful would it be to offer a test capability to be able to inject errors at various points in Dream's request processing cycle to validate their behaviors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially very useful. I have a big project/roadmap category for making Dream apps (and probably Dream itself) really testable. But it's in the queue behind several other things.
I think it's fine then to use a cram test. If really good programmatic Dream testing materializes, I can change the test to use that later, if necessary.