Skip to content

Exposing a port should be idempotent and fix an ephemeral port leak#531

Merged
djs55 merged 9 commits intomoby:masterfrom
djs55:port-expose-idempotent
Apr 27, 2021
Merged

Exposing a port should be idempotent and fix an ephemeral port leak#531
djs55 merged 9 commits intomoby:masterfrom
djs55:port-expose-idempotent

Conversation

@djs55
Copy link
Copy Markdown
Collaborator

@djs55 djs55 commented Apr 21, 2021

Previously attempting to expose an existing port would return an error, which becomes an HTTP 500 internal server error.

This patch makes exposing a port idempotent, just like un-exposing a port already is.

Previously there was a port leak if the user requested any port (0) because we would store 0 in our map, and then look up the real port in Unexpose and fail to find it.

To make the API clearer, we switch to PUT and DELETE (instead of POST) but for backwards compatibility retain the POST handlers.

Minor additional updates:

  • switch from *sync.Mutex to sync.Mutex where we are already using a pointer receiver, since the nil value is a valid mutex
  • run the unit tests with the race detector enabled
  • avoid logging as errors 3 expected conditions (all variations of an EOF on shutdown)

@djs55 djs55 force-pushed the port-expose-idempotent branch 2 times, most recently from 11cab84 to 8b682fe Compare April 23, 2021 10:10
@djs55 djs55 changed the title Exposing a port should be idempotent Exposing a port should be idempotent and fix an ephemeral port leak Apr 27, 2021
djs55 added 8 commits April 27, 2021 14:29
We don't need to use `*sync.Mutex` and allocate separately since the
nil value works and we are using pointer receivers everywhere.

Signed-off-by: David Scott <dave@recoil.org>
Signed-off-by: David Scott <dave@recoil.org>
Previously an attempt to expose an identical port would result in an
HTTP 500 error. After this patch it results in an HTTP 200.

Signed-off-by: David Scott <dave@recoil.org>
This is consistent with the new idempotent semantics.

We keep the server POST handlers for backwards compat.

Signed-off-by: David Scott <dave@recoil.org>
This is consistent with the existing idempotent semantics.

We keep the server POST handlers for backwards compat.

Signed-off-by: David Scott <dave@recoil.org>
Signed-off-by: David Scott <dave@recoil.org>
If a client requests any port (== 0 i.e. INADDR_ANY) the port is resolved
to a concrete port when it is bound.

Previously an attempt to list this port and unexpose it would leak because
we wouldn't find the concrete port in the map.

This patch ensures that the concrete port is stored so the port can be
unexposed properly. This also fixes a potential problem where two forwards
of any port would only result in one actual forward, because the second one
would be assumed to be a duplicate.

Signed-off-by: David Scott <dave@recoil.org>
Signed-off-by: David Scott <dave@recoil.org>
@djs55 djs55 force-pushed the port-expose-idempotent branch from 0950159 to dd16d39 Compare April 27, 2021 13:38
Signed-off-by: David Scott <dave@recoil.org>
@djs55 djs55 force-pushed the port-expose-idempotent branch from dd16d39 to c8bea32 Compare April 27, 2021 13:39
Copy link
Copy Markdown
Collaborator

@ebriney ebriney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@djs55 djs55 merged commit 704adf5 into moby:master Apr 27, 2021
@djs55 djs55 deleted the port-expose-idempotent branch April 27, 2021 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants