typelevel / doobie

Functional JDBC layer for Scala.
https://typelevel.org/doobie/
MIT License
2.18k stars 359 forks source link

Metaspace leak in Transactor #510

Open jarreds opened 7 years ago

jarreds commented 7 years ago

On a project running SBT. Been running test "cycles" on the SBT prompt (test, write code, test, write code, etc.) After a short number of test iterations I get a metaspace out-of-memory exception.

Narrowed the leak down to the Transactor. I'm using a Postgres driver.

If I run the following after each test cycle it prevents the leak:

    import java.sql.DriverManager
    import scala.collection.JavaConverters._
    DriverManager.getDrivers.asScala.foreach(DriverManager.deregisterDriver)

Not 100% certain what's leaking exactly and why, but I'm curious how we could safely integrate some sort of cleanup around this.

Arcovion commented 7 years ago

Also ran into this while testing (on 0.4.2-SNAPSHOT), can confirm your fix works. 👍

Edit: When using H2 with a database file, I get org.h2.jdbc.JdbcSQLException: Database may be already in use: null. Possible solutions: close all other connection(s); use the server mode when running run more than once in sbt shell... could that be related to this issue? It's easily fixed by adding ;AUTO_SERVER=TRUE to the end of the uri, but it seems like that shouldn't be necessary.

tpolecat commented 7 years ago

This is really a consequence of environment leakage when running ~test or ~run … it will always crash eventually, it just happens faster (evidently) when you're adding a JDBC driver to the pile. It would be nice to have a global cleanup hook somewhere in the test machinery.

mads-hartmann commented 7 years ago

@tpolecat I just hit the same issue when I read through the doobie book and played around with the examples (https://github.com/mads-hartmann/random/commit/1c1f96d50de1632812dd0fac762c1adc5c29023f). Would you be open for adding a small "notice" box in the "Our First Program" section of "Connection to a Database"? If so I'd be happy to add it