From 0da29d475cbe2ed0c854103d788df4472b3a75b4 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Fri, 20 May 2022 22:09:25 +0200 Subject: WIP Try fixing memory leaks --- src/main/java/com/lumaserv/bgp/BGPFsm.java | 48 ++++++++++++++++++---- src/main/java/com/lumaserv/bgp/BGPServer.java | 22 ++++++---- src/main/java/com/lumaserv/bgp/BGPSession.java | 11 ++++- .../com/lumaserv/bgp/BGPSessionConfiguration.java | 4 ++ 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/lumaserv/bgp/BGPFsm.java b/src/main/java/com/lumaserv/bgp/BGPFsm.java index 96378a5..4fa35b3 100644 --- a/src/main/java/com/lumaserv/bgp/BGPFsm.java +++ b/src/main/java/com/lumaserv/bgp/BGPFsm.java @@ -16,12 +16,12 @@ public class BGPFsm { public static final int CONFIG_HOLD_TIME = 80; // FIXME use from configuration public static final byte CONFIG_VERSION = 4; - public final State IDLE; - public final State CONNECT; - public final State ACTIVE; - public final State OPEN_SENT; - public final State OPEN_CONFIRM; - public final State ESTABLISHED; + public State IDLE; + public State CONNECT; + public State ACTIVE; + public State OPEN_SENT; + public State OPEN_CONFIRM; + public State ESTABLISHED; @Getter @Setter @@ -40,6 +40,9 @@ public class BGPFsm { TimerTask connectRetryTimerTask; class State { + protected void finalize() throws Throwable { + System.out.println("Finalize State: " + this); + } void setState(State next) { System.out.println("State transition: " + BGPFsm.this + ":" + this + " -> " + next); // if (next == IDLE) @@ -360,7 +363,7 @@ public class BGPFsm { setHoldTimer(holdTime); setState(OPEN_CONFIRM); } else { - System.out.println("Bad asn"); + System.out.println("Bad asn:" + openAsn); try { BGPNotification notification = new BGPNotification() .setMajorErrorCode(BGPNotification.Error.OPEN_MESSAGE_ERROR.getCode()) @@ -502,7 +505,8 @@ public class BGPFsm { public String toString() { return "Established"; } } - BGPFsm() { + public BGPFsm() { + System.out.println("CREATE BGPFsm"); IDLE = new Idle(); CONNECT = new Connect(); ACTIVE = new Active(); @@ -515,6 +519,34 @@ public class BGPFsm { connectRetryTimer = new Timer(); } + public void cleanup() { + // FIXME + IDLE = null; + CONNECT = null; + ACTIVE = null; + OPEN_SENT = null; + OPEN_CONFIRM = null; + ESTABLISHED = null; + currentState = null; + session = null; + if (keepAliveTimerTask != null) { + keepAliveTimerTask.cancel(); + keepAliveTimerTask = null; + } + if (holdTimerTask != null) { + holdTimerTask.cancel(); + holdTimerTask = null; + } + if (connectRetryTimerTask != null) { + connectRetryTimerTask.cancel(); + connectRetryTimerTask = null; + } + } + + protected void finalize() throws Throwable { + System.out.println("Finalize BGPFsm: " + this); + } + private void setKeepAliveTimer() { // System.out.println("setKeepAliveTimer: " + holdTime / 3); diff --git a/src/main/java/com/lumaserv/bgp/BGPServer.java b/src/main/java/com/lumaserv/bgp/BGPServer.java index e95caea..8e38b3a 100644 --- a/src/main/java/com/lumaserv/bgp/BGPServer.java +++ b/src/main/java/com/lumaserv/bgp/BGPServer.java @@ -26,6 +26,10 @@ public class BGPServer implements Runnable { serverSocket = new ServerSocket(port); } + protected void finalize() throws Throwable { + System.out.println("Finalize " + this); + } + private static boolean checkEqual(byte[] a, byte[] b) { if(a == b) return true; @@ -99,6 +103,9 @@ public class BGPServer implements Runnable { } sessions.remove(otherSession); otherSession.dropConnection(); + //otherSession.getFsm().setSession(null); + //otherSession.getFsm().cleanup(); + otherSession.cleanup(); // Accept new connection return false; } else { @@ -132,7 +139,6 @@ public class BGPServer implements Runnable { BGPSession session = new BGPSession(this, socket, config, fsm); fsm.setSession(session); sessions.add(session); - System.out.println("automaticStartPassive"); fsm.getCurrentState().automaticStartPassive(); // System.out.println("tcpConnectionConfirmed in " + fsm.getCurrentState()); @@ -150,12 +156,12 @@ public class BGPServer implements Runnable { } public boolean connect(BGPSessionConfiguration config, String host, int port) throws IOException { - for (BGPSession session : sessions) { - if (session.getConfiguration().equals(config)) { - // Already connected - return false; - } - } + // for (BGPSession session : sessions) { + // if (session.getConfiguration().equals(config)) { + // // Already connected + // return false; + // } + // } try { BGPFsm fsm = new BGPFsm(); @@ -173,6 +179,8 @@ public class BGPServer implements Runnable { public void shutdown() { for(BGPSession session : sessions) { session.automaticStop(); + session.getFsm().cleanup(); + session.cleanup(); } sessions.clear(); if (serverSocket != null) { diff --git a/src/main/java/com/lumaserv/bgp/BGPSession.java b/src/main/java/com/lumaserv/bgp/BGPSession.java index e785e3b..f78467b 100644 --- a/src/main/java/com/lumaserv/bgp/BGPSession.java +++ b/src/main/java/com/lumaserv/bgp/BGPSession.java @@ -70,6 +70,12 @@ public class BGPSession implements Runnable { System.out.println("Finalize BGPSession"); } + public void cleanup() { + fsm.cleanup(); + fsm = null; + //server = null; + } + public void automaticStop() { fsm.getCurrentState().automaticStop(); } @@ -106,7 +112,9 @@ public class BGPSession implements Runnable { } catch(IOException ex) { } dropConnection(); - // TODO Break links between fsm and session! + cleanup(); + //session.getFsm().setSession(null); + //session.getFsm().cleanup(); } else { isAS4Capability = isAdvAS4Capability && open.getAS4Capability().isPresent(); remoteIdentifier = open.getIdentifier(); @@ -216,7 +224,6 @@ System.out.println("Sent open"); } catch (IOException ex) { if (!closed) { fsm.getCurrentState().tcpConnectionFails(); - ex.printStackTrace(); } closed = true; configuration.getListener().onClose(this); diff --git a/src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java b/src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java index 9c24d0a..5ea9237 100644 --- a/src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java +++ b/src/main/java/com/lumaserv/bgp/BGPSessionConfiguration.java @@ -18,4 +18,8 @@ public class BGPSessionConfiguration { byte[] remoteIdentifier; InetAddress remoteAddr; BGPListener listener; + + protected void finalize() throws Throwable { + System.out.println("Finalize BGPSessionConfiguration"); + } } -- cgit v1.2.3