Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide debugger threads from being returned in the threading module #202

Closed
jbelusic opened this issue Jun 13, 2019 · 3 comments
Closed

Hide debugger threads from being returned in the threading module #202

jbelusic opened this issue Jun 13, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@jbelusic
Copy link

This code should go to end but in VS Code does not. In others IDE this run to the end.
Is this BUG or?

Code is:

import logging
import random
import threading
import time

logging.basicConfig(
    level=logging.DEBUG,
    format='(%(threadName)-10s) %(message)s',
)

class Counter(object):
    def __init__(self, start=0):
        self.lock = threading.Lock()
        self.value = start
    def increment(self):
        logging.debug('Waiting for lock')
        self.lock.acquire()
        try:
            logging.debug('Acquired lock')
            self.value = self.value + 1
        finally:
            logging.debug("Releasing")
            self.lock.release()

def worker(c):
    print('Entering worker...')
    for i in range(2):
        print('worker loop:', i)
        pause = random.random()
        logging.debug('Sleeping %0.02f', pause)
        time.sleep(pause)
        c.increment()
    logging.debug('Done')

counter = Counter()
for i in range(2):
    t = threading.Thread(target=worker, args=(counter,))
    t.start()

logging.debug('Waiting for worker threads')
main_thread = threading.currentThread()

try:
    for t in threading.enumerate():
        if t is not main_thread:
            t.join()
except Exception as e:
    print('exception: ', e)
finally:
    print('This is test')

logging.debug('Counter: %d', counter.value)

logging.debug('End of script')
@DonJayamanne DonJayamanne transferred this issue from microsoft/vscode-python Jun 13, 2019
@fabioz
Copy link
Collaborator

fabioz commented Jun 13, 2019

The problem is that you're waiting for all threads to finish (except the main thread) in:

for t in threading.enumerate():
    if t is not main_thread:
        t.join()

So, what happens is that some of the threads returned from threading.enumerate are debugger threads that will not finish until your own threads finish.

The fix for your own code would be joining just the threads that you created.

i.e.:

threads = []
for i in range(2):
    t = threading.Thread(target=worker, args=(counter,))
    threads.append(t)
    t.start()

...

for t in threads:
    t.join()

I'm leaving this open because we may want to hide the debugger threads in the future (I'll update the title to reflect that).

@fabioz fabioz changed the title Bug in thread python 3 (32 Bit) script while running or debugging on win10 64 Bit machine Hide debugger threads from being returned in the threading module Jun 13, 2019
@jbelusic
Copy link
Author

Great, it works, thanx.

@jbelusic jbelusic reopened this Jun 13, 2019
@jbelusic
Copy link
Author

Sorry I am new to GitHub and I accidentally closed this.

@int19h int19h transferred this issue from microsoft/ptvsd May 4, 2020
@int19h int19h added the enhancement New feature or request label Jun 19, 2020
fabioz added a commit to fabioz/debugpy that referenced this issue Oct 29, 2021
@fabioz fabioz closed this as completed in 655e356 Oct 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants