Subprocesses aren't always killed on `pc` command exit
Describe the bug
When the main pc command exits, sometimes output still continues to be generated by subprocesses that the command launches. A symptom of this is that the ports used by Next.js and the server aren't freed, so the next time you try pc run the command once again exits after spawning even more processes. The only way to break out of this loop is to manually do pkill node and pkill python to kill all Python and Node processes running on the machine.
Consider this example. I ran the pc run command, then exited (not pictured). The subprocesses it spawned were not killed, so then when I rerun pc run, it complains that the port is already bound. The command once again exits. Regardless, the Next.js output continues to be generated (see first photo).
Then, I do some other stuff including running git log. Even after running these other commands, the subprocesses originally spawned by pc run continue outputting things:
To Reproduce The best way to reproduce this is to create a fresh Pynecone app using the git HEAD (3e8f6ffbb335e70b5ed80db79c47d552755a616b) and paste in the following code:
import asyncio
import pynecone as pc
class State(pc.State):
"""The app state."""
is_loading: bool = False
counter: int = 0
def toggle_loading(self):
self.is_loading = not self.is_loading
async def increment(self):
await asyncio.sleep(10)
self.counter += 2
def index():
return pc.vstack(
pc.box(State.counter),
pc.button(
"Increment",
on_click=[State.toggle_loading, State.increment, State.toggle_loading],
is_loading=State.is_loading,
),
)
# Add state and page to the app.
app = pc.App(state=State)
app.add_page(index)
app.compile()
After pc run, try using the app, then go to the terminal and Ctrl+C. May not repro the first time. Do this a couple times and you'll note the same issue I faced above.
Expected behavior
pc run gracefully kills its subprocesses on exit.
Specifics
- Python Version: 3.9
- Pynecone Version: 3e8f6ffbb335e70b5ed80db79c47d552755a616b
- OS: macOS Ventura
- Browser (Optional): Arc
Additional context It's unclear whether this is due to specifically the asyncio stuff I’m doing in this code or if it's due to a more general heisenbug affecting all Pynecone apps.
I've noticed this a bit too. This seems to happen for me when using the vscode terminal mostly
Yeah this is annoying, let's prioritize it for the next sprint.
Taking this up
PR merged by @Alek99