Skip to content

Commit

Permalink
Retry when encountering UPnP socket initialization issues.
Browse files Browse the repository at this point in the history
The UPnP socket can not bind to 0.0.0.0 so needs a stable interface
with a configured IP address to bind to.
If the OS has not initialized the network yet, this will fail, so
add a couple of rounds of retries to make this more robust.

This only showed up as an issue with recent distributions using
systemd. I suspect a bug in debian systemd network-online.target actually
not doing as advertised and claiming readiness when in fact DHCP might
still be ongoing.

Fixes #129
  • Loading branch information
hzeller committed Aug 18, 2018
1 parent d0f46f5 commit a836de4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/upnp_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,19 @@ static gboolean initialize_device(struct upnp_device_descriptor *device_def,
char *buf;

rc = UpnpInit(ip_address, port);
/* There have been situations reported in which UPNP had issues
* initializing right after network came up. #129
*/
int retries_left = 60;
static const int kRetryTimeMs = 1000;
while (rc != UPNP_E_SUCCESS && retries_left--) {
usleep(kRetryTimeMs * 1000);
Log_error("upnp", "UpnpInit(ip=%s, port=%d) Error: %s (%d). Retrying... (%ds)",
ip_address, port, UpnpGetErrorMessage(rc), rc, retries_left);
rc = UpnpInit(ip_address, port);
}
if (UPNP_E_SUCCESS != rc) {
Log_error("upnp", "UpnpInit(ip=%s, port=%d) Error: %s (%d)",
Log_error("upnp", "UpnpInit(ip=%s, port=%d) Error: %s (%d). Giving up.",
ip_address, port, UpnpGetErrorMessage(rc), rc);
return FALSE;
}
Expand Down

0 comments on commit a836de4

Please sign in to comment.