Skip to content

Commit 0235638

Browse files
committed
Fix :socket_closed_remotely error on travis
1 parent b252a7d commit 0235638

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

lib/hex/api.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defmodule Hex.API do
2424
http_opts = [ssl: ssl_opts(url), relaxed: true, timeout: @request_timeout] ++ Hex.Utils.proxy_config(url)
2525
opts = [body_format: :binary]
2626
url = String.to_char_list(url)
27+
profile = Hex.State.fetch!(:httpc_profile)
2728

2829
request =
2930
cond do
@@ -37,7 +38,7 @@ defmodule Hex.API do
3738
{url, Map.to_list(headers)}
3839
end
3940

40-
case :httpc.request(method, request, http_opts, opts, :hex) do
41+
case :httpc.request(method, request, http_opts, opts, profile) do
4142
{:ok, response} ->
4243
handle_response(response)
4344
{:error, reason} ->
@@ -101,6 +102,7 @@ defmodule Hex.API do
101102
http_opts = [ssl: ssl_opts(url), relaxed: true, timeout: @request_timeout] ++ Hex.Utils.proxy_config(url)
102103
opts = [body_format: :binary]
103104
url = String.to_char_list(url)
105+
profile = Hex.State.fetch!(:httpc_profile)
104106

105107
body = fn
106108
size when size < byte_size(body) ->
@@ -114,7 +116,7 @@ defmodule Hex.API do
114116

115117
request = {url, Map.to_list(headers), 'application/octet-stream', {body, 0}}
116118

117-
case :httpc.request(method, request, http_opts, opts, :hex) do
119+
case :httpc.request(method, request, http_opts, opts, profile) do
118120
{:ok, response} ->
119121
handle_response(response)
120122
{:error, reason} ->

lib/hex/state.ex

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,35 @@ defmodule Hex.State do
4444
check_cert?: load_config(config, ["HEX_UNSAFE_HTTPS"], :unsafe_https) |> to_boolean |> default(true),
4545
check_registry?: load_config(config, ["HEX_UNSAFE_REGISTRY"], :unsafe_registry) |> to_boolean |> default(true),
4646
hexpm_pk: @hexpm_pk,
47-
registry_updated: false}
47+
registry_updated: false,
48+
httpc_profile: :hex}
49+
end
50+
51+
# Work around for :socket_closed_remotely errors in httpc
52+
# Use a unique profile for each request to avoid races
53+
if Mix.env == :test do
54+
def fetch(:httpc_profile) do
55+
profile = make_ref() |> :erlang.ref_to_list |> List.to_atom
56+
{:ok, _pid} = :httpc_manager.start_link(profile, :only_session_cookies, :stand_alone)
57+
end
4858
end
4959

5060
def fetch(key) do
5161
Agent.get(@name, Map, :fetch, [key])
5262
end
5363

5464
def fetch!(key) do
55-
Agent.get(@name, Map, :fetch!, [key])
65+
case fetch(key) do
66+
{:ok, value} -> value
67+
:error -> raise KeyError, key: key, term: Hex.State
68+
end
5669
end
5770

5871
def get(key, default \\ nil) do
59-
Agent.get(@name, Map, :get, [key, default])
72+
case fetch(key) do
73+
{:ok, value} -> value
74+
:error -> default
75+
end
6076
end
6177

6278
def put(key, value) do

0 commit comments

Comments
 (0)