#πŸ”’ Using VS code and trying to interrupt the terminal with "ctrl + c" doesn't seem to work (unrelated

154 messages Β· Page 1 of 1 (latest)

floral iceBOT
#

@sharp dove

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vapid oxide
#

Can you show a screenshot of your VSCode and terminal?

vapid oxide
# sharp dove

oh ok. If you click in the terminal and hit CTRL + C, what happens?

vapid oxide
# sharp dove

oh hang on, you're using the debug play button instead of typing a command

sharp dove
vapid oxide
#

What happens if you stop the run, then type py bot.py in your terminal and hit enter, then CTRL + C?

#

@sharp dove

sharp dove
#

same thing

#

no output at all in any way shape or form no matter what i click in the terminal

vapid oxide
#

You'll probably need to check your keybinding settings

#

CTRL + SHIFT + P to open up your Command Pallete

#

Type "Keyboard Shortcuts"

#

press enter

reef quarry
#

what libraries u got imported

vapid oxide
#

Then search for workbench.action.terminal.kill

#

Show a screenshot for this this

sharp dove
# reef quarry what libraries u got imported
import logging
import os
import platform
import random
import sys

import aiosqlite
import discord
from discord.ext import commands, tasks
from discord.ext.commands import Context
from dotenv import load_dotenv

from database import DatabaseManager```
floral iceBOT
#

Hey @sharp dove!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
vapid oxide
#

Can you also share what extensions you've installed on VSCode @sharp dove ?

vapid oxide
bleak wasp
#

you need to share your code

#

most importantly where you call client.run

sharp dove
sharp dove
reef quarry
#

this feels like something is catching KeyboardInterrupt for some reason pithink

bleak wasp
#

and thats why we need to see your code

sharp dove
#

ye its code related id guess

#

cuz

bleak wasp
#

it's not vscode's fault

sharp dove
#

yea figured

#

idk what code to share,

bleak wasp
#

the KeyboardInterrupt is definitely being caught and ignored by your code, you need to share your code so we can tell you where this is happening

sharp dove
#

hold on can i screenshare for u

#

rq

#

?

reef quarry
bleak wasp
#

no please just send the code as text

reef quarry
#

!paste

floral iceBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

sharp dove
#

alright

vapid oxide
#

yes

bleak wasp
#

most likely, the issue is around wherever you call bot.run()

reef quarry
sharp dove
#

bot.run is just

bleak wasp
#

where do you run the bot

sharp dove
#

my token within it

#

under that

#

bot.run("token")

bleak wasp
#

if it's really just that, then the problem doesn't appear to be here

#

I wonder what is in database

#

also just to be clear, you are using discord.py and not some derivative library right

sharp dove
#

yes

#

import aiosqlite
import discord
from discord.ext import commands, tasks
from discord.ext.commands import Context
from dotenv import load_dotenv

floral iceBOT
#

Hey @sharp dove!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
sharp dove
#

these r the ones

vapid oxide
#

I don't see anything in the code that would override a KeyboardInterrupt event

reef quarry
#

it would also work if something was catching BaseException for some reason

vapid oxide
#

Can I get all of the files @sharp dove ? I'd like to try and run this myself and see if I get the same issue

sharp dove
#

sure

bleak wasp
#

I'm suspecting that you probably have some threads that aren't closing

sharp dove
bleak wasp
#

no

#

i mean when you run import database

#

what's that

floral iceBOT
#

discord/client.py lines 905 to 911

try:
    asyncio.run(runner())
except KeyboardInterrupt:
    # nothing to do here
    # `​asyncio.run`​ handles the loop cleanup
    # and `​self.start`​ closes all sockets and the HTTPClient instance.
    return```
bleak wasp
#

but asyncio won't deal with threads

sharp dove
#

from database import DatabaseManager

#

this is

#

idk where it imports it otherwise

vapid oxide
reef quarry
#

do u have a database.py

vapid oxide
#

or a database folder?

#

Is bot.py the only thing you have?

bleak wasp
#

it's a folder

sharp dove
#

nah

#

here

vapid oxide
sharp dove
#

yeah

vapid oxide
sharp dove
#

was just gonna zip it

vapid oxide
#

Can you share the repo?

sharp dove
#

it is

#

it's just all thats in it

bleak wasp
#

can you share your cogs

#

the issue is probably in one of those

sharp dove
#

had to find it sorry

vapid oxide
#

Thanks

sharp dove
#

or just here

#

i added a bunch

#

ah it did not let me send

vapid oxide
#

But I think the reason for this is so that it doesn't accidentally get closed while running it

reef quarry
#

it seems to be intended to be used with docker ? pithink

#

so instead of ctrl+c ig it wants u to nuke container running bot instead ?

vapid oxide
reef quarry
#

Β―_(ツ)_/Β―

vapid oxide
#

Same

sharp dove
#

seems pointless blocking it if you're dropping a template for beginners

#

lol

vapid oxide
#

I don't see anywhere on how this bot blocks CTRL + C

reef quarry
#

my bet is the console_handler for logging

vapid oxide
reef quarry
#

that feels like a prime suspect

sharp dove
#

yea but ideally no one would run a bot on their own pc

#

unless ur doing stuff

vapid oxide
#

They could

bleak wasp
#

I just checked and the issue is the database connection

#

it's never closed

sharp dove
#

where would i locate it

#

/ close it

bleak wasp
#

if you add

bot = DiscordBot()
bot.run("token")

# add this
import asyncio
asyncio.run(bot.database.connection.close())

then the bot closes correctly

#

the correct way to do this is to make DatabaseManager a context manager

sharp dove
#

yup

reef quarry
#

ye was about to mention that its weird it isnt one

sharp dove
#

thanks a lot

#

you're a goat

bleak wasp
#

just for the sake of teaching something, you can figure this out by running python -i bot.py

#

so that you get an interactive terminal after the main file reaches its endd

#

and from there you can inspect the list of running threads

#
>>> for t in threading.enumerate():
...     print(f"Thread: {t.name}, Daemon: {t.daemon}, Alive: {t.is_alive()}")
... 
Thread: MainThread, Daemon: False, Alive: True
Thread: Thread-2, Daemon: False, Alive: True
#

lo and behold, theres a second thread still alive

#

there should only be one

vapid oxide
#

Nice detective work @bleak wasp

bleak wasp
#

if you look at the database connection

>>> bot.database.connection
<Connection(Thread-2, started 14876)>
#

then you'll see that this connection is also using "Thread-2"

reef quarry
#

good to know -i exists

sharp dove
#

oh yeah

bleak wasp
#

quite useful indeed

reef quarry
#

man how have i been doing python for like 7y and i still dont know all the useful tools in a default installation

bleak wasp
#

there's always more

sharp dove
#

well holy shit, me getting stupidly pissed i gotta click 3 extra buttons thinking it's vs code

#

_^

bleak wasp
#

also ive been at it for 10 years so clearly the insight all comes in the next 3

sharp dove
#

thank you man, really appreciated

bleak wasp
#

np

floral iceBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.