[SNMP4J] resource leak on failed connect in AgentXSubagent.connect()

Sachs, Larry lsachs at c-cor.com
Sun Mar 4 01:03:32 CET 2007


Hi,

 

I believe that the connect() method in AgentXSubagent is leaking
resources when the openSession() call fails.  Proposed change to
connect()...

 

    public int connect(Address masterAddress, Address localAddress,

                       AgentXSession session) throws IOException {

      AgentXPeer peer = (AgentXPeer) peers.get(masterAddress);

      TransportMapping transport;

      if (peer == null) {

        transport = addMaster(localAddress);

        peer = new AgentXPeer(transport, masterAddress);

      }

      else {

        transport = peer.getTransport();

      }

      peer.setTimeout(session.getTimeout());

      session.setPeer(peer);

      /**@todo improve error status */

      int status = AgentXProtocol.AGENTX_TIMEOUT;

      try {

        status = openSession(transport, masterAddress, session);

        if (status != AgentXProtocol.AGENTX_TIMEOUT) {

          peers.put(masterAddress, peer);

          LOGGER.info("Added new peer
address="+masterAddress+",peer="+peer);

        }

      }

      catch (IOException ex) {

        LOGGER.error(ex);

!       //return AgentXProtocol.AGENTX_ERROR; // don't return, allow to
fall thru for cleanup

      }

      if (status == AgentXProtocol.AGENTX_SUCCESS) {

        sessions.put(new Integer(session.getSessionID()), session);

        LOGGER.info("Opened subagent session successfully: "+session);

      }

+     else {

+       // Cleanup if not successful

+       try {

+         transport.close();

+       }

+       catch (IOException ex) {

+         LOGGER.error(ex);

+       }

+       _agentX.removeTransportMapping(transport);

+     }

      return status;

    }

 

 

Additionally, in DefaultTcpTransportMapping after the while(!stop) loop
terminates in ServerThread.run(), the selector should be closed...

 

*** 736,753 ****

--- 736,760 ----

            catch (NullPointerException npex) {

              // There seems to happen a NullPointerException within the
select()

              npex.printStackTrace();

              logger.warn("NullPointerException within select()?");

            }

            processPending();

          }

          if (ssc != null) {

            ssc.close();

          }

+         if (selector != null) {

+           try {

+             selector.close();

+           } catch (Exception e) {

+             logger.error("Error closing selector", e);

+           }

+         }

        }

        catch (IOException iox) {

          logger.error(iox);

          lastError = iox;

        }

        if (!stop) {

          stop = true;

          synchronized (DefaultTcpTransportMapping.this) {

            server = null;

          }

        }

      }

 

 

Finally, unrelated to the above,  AgentXSubagent.removeMOServer() has an
obvious error (code copied from addMOServer).

 

 

Thanks,

ljs





More information about the SNMP4J mailing list