Skip to content

Fix possible logger leak #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions driver/src/main/java/org/neo4j/driver/internal/NetworkSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
*/
package org.neo4j.driver.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.neo4j.driver.internal.logging.DelegatingLogger;
import org.neo4j.driver.internal.retry.RetryLogic;
import org.neo4j.driver.internal.spi.Connection;
import org.neo4j.driver.internal.spi.ConnectionProvider;
Expand All @@ -47,6 +46,8 @@

public class NetworkSession implements Session, SessionResourcesHandler
{
private static final String LOG_NAME = "Session";

private final ConnectionProvider connectionProvider;
private final AccessMode mode;
private final RetryLogic retryLogic;
Expand All @@ -64,7 +65,7 @@ public NetworkSession( ConnectionProvider connectionProvider, AccessMode mode, R
this.connectionProvider = connectionProvider;
this.mode = mode;
this.retryLogic = retryLogic;
this.logger = logging.getLog( "Session-" + hashCode() );
this.logger = new DelegatingLogger( logging.getLog( LOG_NAME ), String.valueOf( hashCode() ) );
}

@Override
Expand Down Expand Up @@ -378,28 +379,4 @@ private void closeCurrentConnection( boolean sync )
logger.debug( "Released connection " + connection.hashCode() );
}
}

private static List<Throwable> recordError( Throwable error, List<Throwable> errors )
{
if ( errors == null )
{
errors = new ArrayList<>();
}
errors.add( error );
return errors;
}

private static void addSuppressed( Throwable error, List<Throwable> suppressedErrors )
{
if ( suppressedErrors != null )
{
for ( Throwable suppressedError : suppressedErrors )
{
if ( error != suppressedError )
{
error.addSuppressed( suppressedError );
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal.logging;

import org.neo4j.driver.v1.Logger;

import static java.util.Objects.requireNonNull;

public class DelegatingLogger implements Logger
{
private final Logger delegate;
private final String messagePrefix;

public DelegatingLogger( Logger delegate )
{
this( delegate, null );
}

public DelegatingLogger( Logger delegate, String messagePrefix )
{
this.delegate = requireNonNull( delegate );
this.messagePrefix = messagePrefix;
}

@Override
public void error( String message, Throwable cause )
{
delegate.error( messageWithPrefix( message ), cause );
}

@Override
public void info( String message, Object... params )
{
delegate.info( messageWithPrefix( message ), params );
}

@Override
public void warn( String message, Object... params )
{
delegate.warn( messageWithPrefix( message ), params );
}

@Override
public void debug( String message, Object... params )
{
if ( isDebugEnabled() )
{
delegate.debug( messageWithPrefix( message ), params );
}
}

@Override
public void trace( String message, Object... params )
{
if ( isTraceEnabled() )
{
delegate.trace( messageWithPrefix( message ), params );
}
}

@Override
public boolean isTraceEnabled()
{
return delegate.isTraceEnabled();
}

@Override
public boolean isDebugEnabled()
{
return delegate.isDebugEnabled();
}

private String messageWithPrefix( String message )
{
if ( messagePrefix == null )
{
return message;
}
return String.format( "[%s] %s", messagePrefix, message );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;

import org.neo4j.driver.internal.logging.DelegatingLogger;
import org.neo4j.driver.internal.messaging.InitMessage;
import org.neo4j.driver.internal.messaging.Message;
import org.neo4j.driver.internal.messaging.RunMessage;
Expand All @@ -47,6 +48,8 @@

public class SocketConnection implements Connection
{
private static final String LOG_NAME = "Connection";

private final Queue<Message> pendingMessages = new LinkedList<>();
private final SocketResponseHandler responseHandler;
private final AtomicBoolean isInterrupted = new AtomicBoolean( false );
Expand All @@ -57,7 +60,7 @@ public class SocketConnection implements Connection

SocketConnection( BoltServerAddress address, SecurityPlan securityPlan, int timeoutMillis, Logging logging )
{
Logger logger = logging.getLog( "Connection-" + hashCode() );
Logger logger = new DelegatingLogger( logging.getLog( LOG_NAME ), String.valueOf( hashCode() ) );
this.socket = new SocketClient( address, securityPlan, timeoutMillis, logger );
this.responseHandler = createResponseHandler( logger );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

import org.neo4j.driver.internal.logging.DelegatingLogger;
import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.internal.spi.PooledConnection;
import org.neo4j.driver.internal.util.Supplier;
Expand All @@ -39,6 +40,8 @@
*/
public class BlockingPooledConnectionQueue
{
public static final String LOG_NAME = "ConnectionQueue";

/** The backing queue, keeps track of connections currently in queue */
private final BlockingQueue<PooledConnection> queue;
private final Logger logger;
Expand Down Expand Up @@ -162,6 +165,6 @@ private void disposeSafely( PooledConnection connection )

private static Logger createLogger( BoltServerAddress address, Logging logging )
{
return logging.getLog( "ConnectionQueue[" + address + "]" );
return new DelegatingLogger( logging.getLog( LOG_NAME ), address.toString() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.neo4j.driver.v1.Session;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -83,7 +82,7 @@ public void logsMessageWithStacktraceDuringFinalizationIfLeaked() throws Excepti
assertEquals( 1, messageCaptor.getAllValues().size() );

String loggedMessage = messageCaptor.getValue();
assertThat( loggedMessage, startsWith( "Neo4j Session object leaked" ) );
assertThat( loggedMessage, containsString( "Neo4j Session object leaked" ) );
assertThat( loggedMessage, containsString( "Session was create at" ) );
assertThat( loggedMessage, containsString(
getClass().getSimpleName() + "." + testName.getMethodName() )
Expand Down
Loading