'How to terminate main program when thread ends? Still getting waiting for process to detach in python?

I am having a main program which is defined like this:

main.py

def main():

try:
    registry.start_server()
except:
    print("Shutting down the program")
    pass


if __name__ == '__main__':
    main()

registry.start_server() is the method in another module which looks like this:

def start_server():
    t_server = threading.Thread(target=server.start)
    t_server.start()
    try:
        t_server.join()
    except KeyboardInterrupt:
        print("Error")
        raise ValueError

finally:
    fp.close()

server.start is the method in another module which does some listening work in a while(True) manner. I am not sure how to stop the whole program when clicking Stop in PyCharm which is Ctrl + C (Signal). I tried with Event but without success. I get to the main.py by raising an exception when the signal gets caught but that does not terminate the whole program. It shows Waiting for program to detach. The only way is to use SIGKILL. I don't understand where does the program keeps hanging? I have also tried calling sys.exit(0) when the signal gets caught and creating the thread as Deamon but that didnt help either.

EDIT While True method in another module

def start(self, event):

 try:
        while True:
            if event.is_set():
                if self.pubsub.channels:
                    print("It enters here")
                    message = self.pubsub.get_message(True)
                    if message:
                          . 
                          .
                          .
           else:
                return


Solution 1:[1]

To solve the problem, all you need to do is:

  1. let the child-thread exit, and
  2. let main thread join the child-thread.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 seedjyh