Merge pull request #5988 from corda/chrisr3-driver-shutdown

NOTICK: Tidy up driver shutdown so that resources are always released on error.
This commit is contained in:
Matthew Nesbit
2020-02-20 14:34:26 +00:00
committed by GitHub

View File

@ -1129,18 +1129,18 @@ fun <DI : DriverDSL, D : InternalDriverDSL, A> genericDriver(
coerce: (D) -> DI, coerce: (D) -> DI,
dsl: DI.() -> A dsl: DI.() -> A
): A { ): A {
val serializationEnv = setDriverSerialization(driverDsl.cordappsClassLoader) setDriverSerialization(driverDsl.cordappsClassLoader).use { _ ->
val shutdownHook = addShutdownHook(driverDsl::shutdown) val shutdownHook = addShutdownHook(driverDsl::shutdown)
try { try {
driverDsl.start() driverDsl.start()
return dsl(coerce(driverDsl)) return dsl(coerce(driverDsl))
} catch (exception: Throwable) { } catch (exception: Throwable) {
DriverDSLImpl.log.error("Driver shutting down because of exception", exception) DriverDSLImpl.log.error("Driver shutting down because of exception", exception)
throw exception throw exception
} finally { } finally {
driverDsl.shutdown() driverDsl.shutdown()
shutdownHook.cancel() shutdownHook.cancel()
serializationEnv?.close() }
} }
} }
@ -1157,8 +1157,8 @@ fun <DI : DriverDSL, D : InternalDriverDSL, A> genericDriver(
driverDslWrapper: (DriverDSLImpl) -> D, driverDslWrapper: (DriverDSLImpl) -> D,
coerce: (D) -> DI, dsl: DI.() -> A coerce: (D) -> DI, dsl: DI.() -> A
): A { ): A {
val serializationEnv = setDriverSerialization() setDriverSerialization().use { _ ->
val driverDsl = driverDslWrapper( val driverDsl = driverDslWrapper(
DriverDSLImpl( DriverDSLImpl(
portAllocation = defaultParameters.portAllocation, portAllocation = defaultParameters.portAllocation,
debugPortAllocation = defaultParameters.debugPortAllocation, debugPortAllocation = defaultParameters.debugPortAllocation,
@ -1180,18 +1180,18 @@ fun <DI : DriverDSL, D : InternalDriverDSL, A> genericDriver(
djvmCordaSource = defaultParameters.djvmCordaSource, djvmCordaSource = defaultParameters.djvmCordaSource,
environmentVariables = defaultParameters.environmentVariables environmentVariables = defaultParameters.environmentVariables
) )
) )
val shutdownHook = addShutdownHook(driverDsl::shutdown) val shutdownHook = addShutdownHook(driverDsl::shutdown)
try { try {
driverDsl.start() driverDsl.start()
return dsl(coerce(driverDsl)) return dsl(coerce(driverDsl))
} catch (exception: Throwable) { } catch (exception: Throwable) {
DriverDSLImpl.log.error("Driver shutting down because of exception", exception) DriverDSLImpl.log.error("Driver shutting down because of exception", exception)
throw exception throw exception
} finally { } finally {
driverDsl.shutdown() driverDsl.shutdown()
shutdownHook.cancel() shutdownHook.cancel()
serializationEnv?.close() }
} }
} }