powered by Jive Software

[bug] does not release socket resource

File:

org.jivesoftware.openfire.net.BlockingAcceptingMode.java

Code:

@Override
  public void run() {
        while (notTerminated) {
            try {
                Socket sock = serverSocket.accept();
                if (sock != null) {
                    Log.debug("Connect " + sock.toString());
                    SocketReader reader =
                            connManager.createSocketReader(sock, false, serverPort, true);
                    Thread thread = new Thread(reader, reader.getName());
                    thread.setDaemon(true);
                    thread.setPriority(Thread.NORM_PRIORITY);
                    thread.start();
                }
            }
            catch (IOException ie) {
                if (notTerminated) {
                    Log.error(LocaleUtils.getLocalizedString("admin.error.accept"),
                            ie);
                }
            }
            catch (Throwable e) {
                Log.error(LocaleUtils.getLocalizedString("admin.error.accept"), e);
            }
        }
    }

Socket does not released when throw Exceptions in openfire 3.9.3

What code change do you suggest? This file hasn’t been touched in four years

Openfire/BlockingAcceptingMode.java at master · igniterealtime/Openfire · GitHub

Suggest Code:

@Override
  public void run() {
  while (notTerminated) {
  Socket sock = null;
  try {
  sock = serverSocket.accept();
  if (sock != null) {
  Log.debug("Connect " + sock.toString());
  SocketReader reader = connManager.createSocketReader(sock,
  false, serverPort, true);
  Thread thread = new Thread(reader, reader.getName());
  thread.setDaemon(true);
  thread.setPriority(Thread.NORM_PRIORITY);
  thread.start();
  }
  } catch (IOException ie) {
  if (notTerminated) {
  Log.error(LocaleUtils
  .getLocalizedString("admin.error.accept"), ie);
  }
  } catch (Throwable e) {
  Log.error(LocaleUtils.getLocalizedString("admin.error.accept"),
  e);
  } finally {
  if (sock != null) {
  try {
  sock.close();
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  }
  }
  }

This code will be better,When throw Exceptions,close the socket resource,and What do you think of it?

try catch finally