Skip to content

Commit

Permalink
Merge pull request #32 from utkarshkukreti/handle-timeout
Browse files Browse the repository at this point in the history
Fix crash due to TCP receive timeout
  • Loading branch information
s3cur3 authored Sep 15, 2024
2 parents 5e879df + 1b84b1f commit 550265e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.3.1

- Fix crash when TCP receive times out (`Whois.lookup/2` now correctly returns `{:error, :timed_out}`)

## v0.3.0

- Add support for custom timeouts via the `:connect_timeout` and `:recv_timeout`
Expand Down
2 changes: 1 addition & 1 deletion lib/whois.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ defmodule Whois do

case :gen_tcp.recv(socket, 0, timeout) do
{:ok, data} -> recv(socket, acc <> data, opts)
{:error, :etimedout} -> {:error, :timed_out}
{:error, timeout} when timeout in [:etimedout, :timeout] -> {:error, :timed_out}
{:error, :closed} -> acc
end
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Whois.Mixfile do
def project do
[
app: :whois,
version: "0.3.0",
version: "0.3.1",
elixir: "~> 1.12",
consolidate_protocols: Mix.env() != :test,
elixirc_paths: elixirc_paths(Mix.env()),
Expand Down
5 changes: 5 additions & 0 deletions test/whois_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ defmodule WhoisTest do
assert %NaiveDateTime{} = record.expires_at
end

@tag :live
test "lookup/1 handles timeouts" do
assert {:error, :timed_out} = Whois.lookup("google.com", recv_timeout: 0)
end

defp wait, do: Process.sleep(2500)
end

Expand Down

0 comments on commit 550265e

Please sign in to comment.