Skip to content
This repository was archived by the owner on Jul 1, 2021. It is now read-only.

Commit 736622f

Browse files
committed
Fix exception escaping Upnp Service
1 parent 872c4d8 commit 736622f

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

newsfragments/1410.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Catch exception leaking out of the `UpnpService` and log it as warning.

trinity/components/builtin/upnp/nat.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,30 @@ async def _add_nat_portmap(self, upnp_dev: upnpclient.upnp.Device) -> str:
130130

131131
external_ip = upnp_dev.WANIPConn1.GetExternalIPAddress()['NewExternalIPAddress']
132132
for protocol, description in [('TCP', 'ethereum p2p'), ('UDP', 'ethereum discovery')]:
133-
upnp_dev.WANIPConn1.AddPortMapping(
134-
NewRemoteHost=external_ip,
135-
NewExternalPort=self.port,
136-
NewProtocol=protocol,
137-
NewInternalPort=self.port,
138-
NewInternalClient=internal_ip,
139-
NewEnabled='1',
140-
NewPortMappingDescription=description,
141-
NewLeaseDuration=self._nat_portmap_lifetime,
142-
)
143-
self._mapping = PortMapping(
144-
'%s:%d' % (internal_ip, self.port),
145-
'%s:%d' % (external_ip, self.port),
146-
)
147-
self.logger.info("NAT port forwarding successfully set up: %r", self._mapping)
133+
try:
134+
upnp_dev.WANIPConn1.AddPortMapping(
135+
NewRemoteHost=external_ip,
136+
NewExternalPort=self.port,
137+
NewProtocol=protocol,
138+
NewInternalPort=self.port,
139+
NewInternalClient=internal_ip,
140+
NewEnabled='1',
141+
NewPortMappingDescription=description,
142+
NewLeaseDuration=self._nat_portmap_lifetime,
143+
)
144+
except upnpclient.soap.SOAPError as exc:
145+
self.logger.warning(
146+
"Failed to setup port mapping for %s/%s: %s",
147+
protocol,
148+
description,
149+
exc
150+
)
151+
else:
152+
self._mapping = PortMapping(
153+
'%s:%d' % (internal_ip, self.port),
154+
'%s:%d' % (external_ip, self.port),
155+
)
156+
self.logger.info("NAT port forwarding successfully set up: %r", self._mapping)
148157
return external_ip
149158

150159
async def _discover_upnp_devices(self) -> AsyncGenerator[upnpclient.upnp.Device, None]:

0 commit comments

Comments
 (0)