Skip to content

Commit

Permalink
Temporarily disabled shutdown hooks as they are causing app locks
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Jun 6, 2024
1 parent a87d2a1 commit 1bb5939
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ target/
testdb/
benchmark/imdb/
benchmark/data/
backup/
backup/
backup.zip
33 changes: 21 additions & 12 deletions core/src/main/scala/lightdb/LightDB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java.util.concurrent.atomic.AtomicBoolean
import scribe.cats.{io => logger}

import scala.concurrent.duration.{DurationInt, FiniteDuration}
import scala.util.Try

abstract class LightDB {
def directory: Path
Expand Down Expand Up @@ -107,16 +108,21 @@ abstract class LightDB {
}

private lazy val disposeThread = new Thread {
override def run(): Unit = dispose().unsafeRunSync()
override def run(): Unit = Try {
try {
dispose().unsafeRunSync()
} catch {
case t: Throwable => scribe.error(s"Failure disposing during shutdown hook", t)
}
}
}

// TODO: Figure out why shutdown hook causes system to get stuck
private def addShutdownHook(): Unit = {
Runtime.getRuntime.addShutdownHook(disposeThread)
// Runtime.getRuntime.addShutdownHook(disposeThread)
}

private def removeShutdownHook(): Unit = {
Runtime.getRuntime.removeShutdownHook(disposeThread)
}
private def removeShutdownHook(): Unit = {} //Try(Runtime.getRuntime.removeShutdownHook(disposeThread))

private def recursiveUpdates(): IO[Unit] = for {
_ <- IO.sleep(updateFrequency)
Expand Down Expand Up @@ -151,13 +157,16 @@ abstract class LightDB {

def update(): IO[Unit] = collections.map(_.update()).sequence.map(_ => ())

def dispose(): IO[Unit] = for {
_ <- commit()
_ <- collections.map(_.dispose()).parSequence
_ <- stores.map(_.dispose()).parSequence
_ = removeShutdownHook()
_ = _disposed.set(true)
} yield ()
def dispose(): IO[Unit] = if (_disposed.compareAndSet(false, true)) {
for {
_ <- commit()
_ <- collections.map(_.dispose()).parSequence
_ <- stores.map(_.dispose()).parSequence
_ = removeShutdownHook()
} yield ()
} else {
IO.unit
}

protected object stored {
def apply[T](key: String,
Expand Down

0 comments on commit 1bb5939

Please sign in to comment.