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

Cleaning up threads #12

Open
legate opened this issue Sep 19, 2015 · 5 comments · May be fixed by #24
Open

Cleaning up threads #12

legate opened this issue Sep 19, 2015 · 5 comments · May be fixed by #24

Comments

@legate
Copy link

legate commented Sep 19, 2015

I tried running the EventManager sample at http://peers.sourceforge.net/files/html/peers.html. If I make a call, then hang up, even though the main thread completes, the program does not stop because there are still some threads running (TransactionManager, InviteHandler, TransportManager). Is there a way I can stop the remaining threads so the program will exit cleanly?

@ymartineau
Copy link
Owner

hi,

here is what is done in peers-gui, in EventManager to close the remaining
threads when user closes the main window:

            try {
                userAgent.unregister();
            } catch (Exception e) {
                logger.error("error while unregistering", e);
            }
            closed = true;
            try {
                Thread.sleep(3 * RFC3261.TIMER_T1);
            } catch (InterruptedException e) {
            }
            System.exit(0);

I think you can do the same.

The example provided in documentation is just a basic example to start
using peers. All use cases are not considered for this demo in usage in
documentation.

On Sat, Sep 19, 2015 at 2:41 AM, legate [email protected] wrote:

I tried running the EventManager sample at
http://peers.sourceforge.net/files/html/peers.html. If I make a call,
then hang up, even though the main thread completes, the program does not
stop because there are still some threads running (TransactionManager,
InviteHandler, TransportManager). Is there a way I can stop the remaining
threads so the program will exit cleanly?


Reply to this email directly or view it on GitHub
#12.

@johnythefarmer
Copy link

johnythefarmer commented Jun 15, 2016

Experienced similiar isssue. Using peers library for testing of VoIP system, so when the test is over, I cannot call System.exit. Most of the threads that are still going, are TimerThreads (InviteHandler, TransactionManager and RegisterHandler have their timers scheduled, but never cancelled)

@johnythefarmer
Copy link

The code in pull request is great, but does not solve the register handler problem. Even though the timer is cancelled in RegisterHandler.unregister, there are still TimerThreads going, when the useragent unregisters. The reason is that when RegisterHandler.successResponseReceived is called, new Timer is created and task scheduled, but the old timer is never cancelled and refference to it is lost. This could be solved by calling:
if (timer != null) {
timer.cancel();
}

right before creating new Timer.

@paraplexed
Copy link
Contributor

Good catch. I updated the PR to close the timer when refreshing, if a timer already exists.

@tamtc84
Copy link

tamtc84 commented Oct 17, 2019

successResponseReceived
I have same probleam: can't stop the threads after userAgent.close().
In RegisterHandler.successResponseReceived(), i change

if (!unregisterInvoked) {
            	if (delay == -1) {
            		delay = Integer.parseInt(expires) - REFRESH_MARGIN;
            	}
                timer = new Timer(getClass().getSimpleName()
                        + " refresh timer");
                timer.schedule(new RefreshTimerTask(), delay * 1000);
            }

to

if (!unregisterInvoked) {
            	if (delay == -1) {
            		delay = Integer.parseInt(expires) - REFRESH_MARGIN;
            	}
                
                if (timer != null) {
                    timer.cancel();
                }
                
                timer = new Timer(getClass().getSimpleName()
                        + " refresh timer");
                timer.schedule(new RefreshTimerTask(), delay * 1000);
            }

But propram stil running, not terminate.

Please help me to fix it.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants