[SNMP4J] InterruptedException not handled properly

Jonathan Louie louie at cs.stanford.edu
Thu Aug 7 02:07:18 CEST 2008


I have a thread that does a SNMP get request periodically in a loop.
Interrupting the thread should cause the loop to stop, but doesn't.
Here's some pseudo-code:

public class SnmpBug  {
		
	public void start() throws IOException {
		TransportMapping transport = new DefaultUdpTransportMapping();
		transport.listen();
		final Snmp snmp = new Snmp(transport);

			thread = new Thread() {
				@Override
				public void run() {
					try {
						while (!isInterrupted()) {
							ResponseEvent response = snmp.get(requestPdu, target);
							// handle response

							Thread.sleep(5000);
						}
					} catch (InterruptedException ie) {
						// interrupted, return
					}
					// this line is never reached
				}
			};
			thread.start();
		
	}

	public void stop() {
		if (thread != null) {
			thread.interrupt();
		}
	}
}

The bug is in Snmp.java line 820.  The library method could throw
InterruptedException, or set interrupted to true.

A good article on how to handle InterruptedException is here:
http://www.ibm.com/developerworks/java/library/j-jtp05236.html


-- 
Jonathan Louie <louie at cs.stanford.edu>
 http://www.eff.org/



More information about the SNMP4J mailing list