[SNMP4J] RuntimeException on shutdown

forum at friendlysnmp.org forum at friendlysnmp.org
Thu Nov 15 18:08:13 CET 2018


I implemented notification 'appShutdown' as follows:
1. NotificationOriginatorImpl -> notify() with 'appShutdown' 
notification.
2. Snmp -> close() to release all resources.
3. Close application.

The result is exception (line numbers are for SNMP4J v2.6.2):
java.lang.RuntimeException: java.net.SocketException: socket closed
   at 
org.snmp4j.transport.DefaultUdpTransportMapping$ListenThread.run(DefaultUdpTransportMapping.java:455)
   at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: socket closed
   at 
java.net.DualStackPlainDatagramSocketImpl.socketReceiveOrPeekData(Native 
Method)
   at 
java.net.DualStackPlainDatagramSocketImpl.receive0(DualStackPlainDatagramSocketImpl.java:124)
   at 
java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:143)
   at java.net.DatagramSocket.receive(DatagramSocket.java:812)
   at 
org.snmp4j.transport.DefaultUdpTransportMapping$ListenThread.run(DefaultUdpTransportMapping.java:397)
   at java.lang.Thread.run(Thread.java:748)

I think the reason for the exception thrown is the 'appShutdown' 
notification confirmation packet comes *after* all resources are closed. 
The packet is processed in DefaultUdpTransportMapping.ListenThread.run() 
line-397 socketCopy.receive(packet).
This method throws SocketException because the resources are closed, and 
the catch at line-455 re-throws RuntimeException.
As the result DefaultUdpTransportMapping.ListenThread is not stopped and 
application fails to exit.

What would you suggest to fix the exception in this scenario?
Maybe additional boolean flag in TransportMapping could be added to 
signal WorkerTask not to process any packets if TransportMapping is 
closed?

Thanks,
Mark



More information about the SNMP4J mailing list