Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6697255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:26:22+00:00 2026-05-26T06:26:22+00:00

I have a service that spawns threads. The threads are started by providing a

  • 0

I have a service that spawns threads.
The threads are started by providing a target function.
It would appear that the thread doesn’t “die” when the function ends. I know this because the thread makes some SSH connections with Paramiko (via Fabric), and if I do an lsof I see the SSH connections are still active after the function completes.
How can I make sure that a thread dies when its target function completes?

Here is an example of what I am working with:

from time import sleep
from threading import Thread
from fabric.api import run, settings

def thread_func(host):
    with settings(host_string=host):
        run('ls -lht /tmp')

def spawn_thread(host):
    t = Thread(
        target=thread_func,
        args=(host,)
    )
    t.start()

spawn_thread('node1.example.com')
while True:
    sleep(1)

And if I run sudo lsof | grep ssh in another terminal while the above code is in its infinite loop I’ll see the following, even after I know that the thread should not exist anymore:

python    6924      daharon    3u     IPv4             170520        0t0        TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python    6924      daharon    5u     IPv4             170524        0t0        TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
python    6924 6930 daharon    3u     IPv4             170520        0t0        TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python    6924 6930 daharon    5u     IPv4             170524        0t0        TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
python    6924 6932 daharon    3u     IPv4             170520        0t0        TCP 10.1.1.173:47368->node1.example.com:ssh (ESTABLISHED)
python    6924 6932 daharon    5u     IPv4             170524        0t0        TCP 10.1.1.173:47369->node1.example.com:ssh (ESTABLISHED)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:26:22+00:00Added an answer on May 26, 2026 at 6:26 am

    Are you sure your fabric module is doing the ssh only for the duration of the ls command. That is, it should be doing the equivalent of

    ssh host ls -lht /tmp

    This commandline will open a remote shell to run the ls -lht command and then shutdown.

    But I suspect the Fabric library might be doing the equivalent of:

    ssh host

    host$ ls -lht /tmp
    .
    .

    Of course it’s not providing a real tty but there are different ssh options that allow keeping a connection open without an interactive tty. This would be desirable in certain cases (e.g., if you run lots of commands on the same host, this technique will reuse the existing ssh session instead of opening new session every time. Check the documentation for arguments to enable or disable such session caching.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have taken over some .NET 1.1 Windows Service code that spawns threads to
We have a Windows Service written in C#. The service spawns a thread that
I have a .NET Windows Service which spawns a thread that basically just acts
HI Guys, I have created a windows service which spawns three threads.The first thread
I have a Windows service written in C# that spawns several worker threads. Those
I have a service that spawns threads. And i may have a leak of
I have a windows service that spawns off around 60 threads when it starts.
I have written a Windows service that spawns a separate process. This process creates
I have a service that I would like it to become single instance, because
I have an MFC application that spawns a number of different worker threads and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.