varabyte / kobweb

A modern framework for full stack web apps in Kotlin, built upon Compose HTML
https://kobweb.varabyte.com
Apache License 2.0
1.46k stars 65 forks source link

Sometimes it seems like a kobweb server starts after one stops (on Windows?) #196

Closed bitspittle closed 1 year ago

bitspittle commented 1 year ago

Example repro steps:

1. cd examples/jb/counter
2. ./gradlew clean
3. ./gradlew --stop
4. kobweb run -> Server started PID=13860
5. q -> server stopped PID=13860
6. kobweb stop -> Did not detect running server.
7. ./gradlew clean -> Task failed / java.io.IOException: Unable to delete directory '...\kobweb-templates\examples\jb\counter\build' / Failed to delete some children. This might happen because a process has files open or has its working directory set in the target directory.
7.5 (sometimes). kobweb stop -> Did not detect running server.
8. kobweb stop -> Server stopped PID=20940
bitspittle commented 1 year ago

Possible relevant error?

Starting a Kobweb server (development)..

Press Q anytime to cancel.
Exception in thread "main" com.charleskorn.kaml.EmptyYamlDocumentException at <root> on line 1, column 1: The YAML document is empty.
        at com.charleskorn.kaml.YamlParser.<init>(YamlParser.kt:41)
        at com.charleskorn.kaml.Yaml.decodeFromReader(Yaml.kt:48)
        at com.charleskorn.kaml.Yaml.decodeFromString(Yaml.kt:40)
        at com.varabyte.kobweb.server.api.ServerStateFile$1.invoke(ServerState.kt:20)
        at com.varabyte.kobweb.server.api.ServerStateFile$1.invoke(ServerState.kt:20)
        at com.varabyte.kobweb.project.io.KobwebReadableTextFile.getContent(KobwebFile.kt:56)
        at com.varabyte.kobweb.cli.run.RunKt$handleRun$1$3$4.invokeSuspend(Run.kt:212)
...
        at com.varabyte.kobweb.cli.run.RunKt.handleRun(Run.kt:63)
        at MainKt$main$Run.execute(main.kt:90)
        at kotlinx.cli.ArgParser.parse(ArgParser.kt:657)
        at kotlinx.cli.ArgParser.parse(ArgParser.kt:530)
        at MainKt.main(main.kt:115)
bitspittle commented 1 year ago

Another possible relevant error?

> Task :kobwebStop
A Kobweb server running at http://localhost:8080 (PID = 6548) was stopped

BUILD SUCCESSFUL in 29s
1 actionable task: 1 executed
Exception in thread "DefaultDispatcher-worker-1" java.lang.NullPointerException: line must not be null
        at com.varabyte.kobweb.common.io.StreamUtilsKt$consumeAsync$1.invokeSuspend(StreamUtils.kt:24)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
bitspittle commented 1 year ago

And finally, I'm getting this error when trying to run ./gradlew clean after running kobweb stop:

> Task :clean FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':clean'.
> java.io.IOException: Unable to delete directory 'C:\Users\bitspittle\tmp\kobweb-templates\counter\build'
    Failed to delete some children. This might happen because a process has files open or has its working directory set in the target directory.
    - C:\Users\bitspittle\tmp\kobweb-templates\counter\build\tmp\expandedArchives
    - C:\Users\bitspittle\tmp\kobweb-templates\counter\build\tmp
    New files were found. This might happen because a process is still writing to the target directory.
    - C:\Users\bitspittle\tmp\kobweb-templates\counter\build\tmp\expandedArchives\kotlin-stdlib-js-1.7.20.jar_58a1f849599c38af0f93386c64580ed6
    - C:\Users\bitspittle\tmp\kobweb-templates\counter\build\tmp\expandedArchives\kotlin-stdlib-js-1.7.20.jar_58a1f849599c38af0f93386c64580ed6\META-INF
    - C:\Users\bitspittle\tmp\kobweb-templates\counter\build\tmp\expandedArchives\kotlin-stdlib-js-1.7.20.jar_58a1f849599c38af0f93386c64580ed6\META-INF\MANIFEST.MF

The line New files were found. This might happen because a process is still writing to the target directory. is particularly worrisome.

bitspittle commented 1 year ago

Finally played around with this on a Windows machine.

What I found most interesting was if I stopped a running server and then waited a while, a new server wouldn't start. It was only by doing a clean that a new server started.

My current hypothesis is that on Windows Gradle is still running in continuous mode (on the kobwebStart task). As soon as you clean the project, it deletes files, which causes kobwebStart to rerun, building stuff as a side effect.

In other words, these lines: https://github.com/varabyte/kobweb/blob/bed0bafb7b575e42f43ed383127f0f351670d3ab/cli/kobweb/src/main/kotlin/com/varabyte/kobweb/cli/run/Run.kt#L192

startServerProcess.destroy()
startServerProcess.waitFor()

might not be working to stop Gradle running in continuous mode.

I'm not sure how to confirm this is happening since, once I've destroyed the process, I am no longer informed about what Gradle is doing. I'm currently guessing just based on the fact that a server gets restarted and I can't imagine any other way that could be happening besides a phantom running Gradle task.