pthread_cancel failure


 
Thread Tools Search this Thread
Top Forums Programming pthread_cancel failure
# 1  
Old 02-08-2012
pthread_cancel failure

I'm running a simple web server and seem to be having a problem canceling sessions.


When a new request is received I start a thread to handle that session's requests. Since I want to keep the pipe open for a long time - 10 minutes or maybe 2 hours - I also have a session manager that checks for inactivity. When the time is up I want to cancel the session thread.


The problem is I don't think the thread is getting canceled and I'm leaking memory with zombie sessions. I've set the threads for asynchronous cancellation but I'm getting a return code of 3 and I should get all zeroes per the manual.


Server code:
pthread_create( &NewSessionThread, NULL, &HTTP_Server_Session, (void *) (NewSessionListItem));






Session code:
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, oldstate);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, oldtype);






Session manager code:
CancelResult = pthread_cancel(SessionPointer->SessionThreadID);


fprintf(CrashLog, "HTTP pthread cancellation result (0, success - other not): %d\n", CancelResult);
if (CancelResult == 3)
fprintf(CrashLog, "HTTP pthread cancellation error: %s\n", strerror(errno));








Logfile sample result:
HTTP pthread cancellation result (0, success - other not): 3
HTTP pthread cancellation error: Success




I guess I have three questions:
Why isn't the cancellation working?
Why is the error string “Success” when the result is not 0?
How should I try to fix it?


Thanks!
# 2  
Old 02-09-2012
If the sessions are blocking on a read() they should be at a cancellation point, no problem.
Are you invoking any kind of cleanup -- pthread_cleanup_push & pop? Are you leaving malloc'd memory behind?

I think you ARE cancelling but not cleaning up something. I dunno what precisely.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-09-2012
Hi Jim,

Thanks for your reply.

It's good to hear that threads blocked on a read() are at a cancellation point.

Some threads also flow to their exit and mark themselves for immediate cancellation. Should I *not* cancel these since they've exited? Maybe I'm canceling threads that have exited and that is giving me the '3' return code.

I'm not doing any cleanup in the session itself since I don't allocate any memory there. I do free its associated structure in the Session Manager. This structure was created by the server - the NewSessionListItem - and was passed to the session.

I didn't originally mention it, but I do the same things (Server, Session, SessionManager) for SMTP and FTP as well. And I've got code to generate any number of FTP sessions so I can perform high-volume testing to flush out the leaks. Do you think I should try the shotgun approach of making hundreds of sessions per minute or continue with the rifle approach of trying to find the precise problem?

Thanks,

John
# 4  
Old 02-09-2012
Already "dead" threads can be reaped with pthread_join, if they are not set to detached.
And yes, you should not cancel a dead thread, it will return an error.

You can also call pthread_cancel on the value returned by pthread_self, which is probably what you want to do. This cancels the thread, then if it was not detached, you call pthread_join to clean up. Otherwise you leave OS memory allocated to the LWP that was the now defunct thread.

In all honesty, I'm not getting what you are doing exactly. And yes, to solve your problem work on one problem only until you clear it. This kind of stuff can crash the system when no more process masthead header slots (LWP's use them too) are left because they have not been cleaned up after. What is PTHREAD_MAX on your system?
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 02-13-2012
Thanks again, Jim.

Just wanted to let you know I'm still working the problem and your points are highly impactful to me.

I'll get back with substantive results soon...

Thanks,

John
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Backup Failure

Hi Team, Am new to solaris. There is a script in the crontab that issues backup at a scheduled time. It fails alway. here are some logs. Can anyone please help why backup fails.: sdpuser@sdp3a>dmesg ... (4 Replies)
Discussion started by: andersonedouard
4 Replies

2. UNIX for Dummies Questions & Answers

boot up failure unix sco after power failure

hi power went out. next day unix sco wont boot up error code 303. any help appreciated as we are clueless. (11 Replies)
Discussion started by: fredthayer
11 Replies

3. UNIX for Advanced & Expert Users

su failure

Usually when su rejects an attempt to switch user it responds with "Sorry" but with a certain username on some unix servers the response is "Killed". I'm guessing the su accepted the username/password but refused to spawn child shell with the specified username. What's causing this and what has to... (8 Replies)
Discussion started by: twk
8 Replies

4. Solaris

failure in lucreate

Earlier live upgrade from solaris 8 to solaris 10 5/08 was not permitted . Later by document 250526 dated Feb 2009 , it permitted and fllowing s8p7zip.tar and following solution 206844 . While installing SUNWluu,SUNWlur,SUNWlucfg , I had a partial failure . But pkginfo shows the information... (5 Replies)
Discussion started by: Hitesh Shah
5 Replies

5. UNIX for Advanced & Expert Users

Memory leak while using pthread_cancel()

I tried to execute a sample pthread program to cancel a newly created one using pthread_cancel(). but using valgrind on my code shows some memory leak. My Code: #include "iostream" #include "unistd.h" #include "pthread.h" #include "signal.h" using namespace std; void handler(int); void*... (4 Replies)
Discussion started by: kcr
4 Replies

6. Linux

Boot failure

Hi all I used a dual boot operating system and it works fine for me. Now , i install a Ati radeon 9250 Agp card on my system and this results in boot failure of fedora 6. The graphics card is working fine with windows XP , i.e i have no compatibility issues.The system also refuses to boot when i... (2 Replies)
Discussion started by: joshighanshyam
2 Replies

7. UNIX for Advanced & Expert Users

subsystem failure

aix 4.3 I get the following error from the errpt log BD797922 0306170008 P H enclosure0 SUBSYSTEM FAILURE I'm not clear on this kind of error. It never happened to me. What do this errors mean, and how do I clear or solve them? Any help is appreciated. (2 Replies)
Discussion started by: andwhat
2 Replies

8. Programming

Question: pthread_cancel() and printf()

Hello! First of all, sorry for my English, I'm not a native English speaker. I know, that printf() function uses write() function. "man cancellation" says that write() function is a cancellation point. But when I call pthread_cancel() for my thread, which calls printf() in infinite cycle, it... (4 Replies)
Discussion started by: prankster
4 Replies

9. Programming

Use of pthread_cancel()

Hi all, I am working on a piece of multi threaded code, where I have a main thread looping in increments of 5ms, while it is waiting on a helper thread which is writing to a remote location. This helper thread keeps updating the main thread about progress of the write operation and the moment we... (1 Reply)
Discussion started by: ravneetd
1 Replies

10. Programming

ld failure

Hi, I am using gmake to compile a c program with a makefile. The make file runs ld. I get the following error jsh1035c:/users/egate453/admegate/kapil/samples $ gmake -e -f GNUmakefile queue_c gmake -f ./GNUmakefile queue_c in_objdir=1 build_root=/users/egate453/admegate/kapil/samples... (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question