opentracing-contrib / opentracing-contrib/java-jdbc

memory leak using c3p0 pooling library

Open
#83 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
86
Forks
53
Avg merge
1d 3h
Merged PRs (30d)
18

Description

I am using a custom fork of the Java Special Agent. We have a service that relies on the Quartz scheduler, which uses c3p0 (v0.9.1.1). With the tracing agent applied, the service leaks memory until it throws an OOM error.

A heap dump showed that TracingPreparedStatements were being leaked. I tracked this to the use of the WrapperProxy and the fact that, given a Proxy instance of TracingConnection or TracingStatement, a call to obj.equals(obj) will always return false. This is because the WrapperProxy is delegating all method calls to the wrapper or the original object, but these don't know how to handle the added Proxy/WrapperProxy layers for the equals method. This leads to c3p0 not being able to clean up its collections for the connections/statements.

I was able to fix this by adding a custom equals method to TracingStatement:

  @Override
  public boolean equals(Object obj) {
	if (this == obj) {
		return true;
	} else if (obj == null) {
		return false;
	} else if (WrapperProxy.isWrapper(obj, getClass())) {
		return obj.equals(this); // trick to get through the WrapperProxy/Proxy instances
	} else if (getClass() != obj.getClass()) {
		return false;
	} else {
		TracingStatement other = (TracingStatement) obj;
		if (statement == null) {
			if (other.statement != null) {
				return false;
			}
		} else if (statement != other.statement && !statement.equals(other.statement)) {
			return false;
		}
	}
	return true;
  }

and to TracingConnection:

  @Override
  public boolean equals(Object obj) {
	if (this == obj) {
		return true;
	} else if (obj == null) {
		return false;
	} else if (WrapperProxy.isWrapper(obj, getClass())) {
		return obj.equals(this); // trick to get through the WrapperProxy/Proxy instances
	} else if (getClass() != obj.getClass()) {
		return false;
	} else {
		TracingConnection other = (TracingConnection) obj;
		if (connection == null) {
			if (other.connection != null) {
				return false;
			}
		} else if (connection != other.connection && !connection.equals(other.connection)) {
			return false;
		}
	}
	return true;
  }

These may not be 100% hardened, but they got me past the memory leak with c3p0. I thought I should be a good citizen and open an issue regarding this.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating TracingConnection, TracingStatement, and WrapperProxy, then reproduce the c3p0 0.9.1.1 pooling scenario described here. Verify that equality works through the proxy layers and that pooled statements and connections are released without the reported leak; no test path is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.