Parent thread terminates before child threads are complete. Should handles leak?
Posted on 2008-10-10
I have a very tricky threading issue and I think I've narrowed it down:
My program creates a thread to query a database for information. If the database contains records that match, separate threads are spawned to generate email alerts for the records.
This seems to work well in normal operating environments (alert records create the alert emails).
When the mailserver is overloaded, slow, or shut down, the child threads do not complete in a timely fashion (waiting on mailserver). The parent database query thread completes and terminates, but its child threads are still waiting for a (potentially non-existant) mailserver.
The child threads eventually complete (either successfully or gracefully fail), but handles are leaked.
I have tested this code with a shut-down mailserver, and handles leak anytime the parent thread completes before the child threads error out.
Handles do NOT leak if I remove the threading and attempt to send the emails non-multithreaded (they fail one at a time).
Is this type of threading/handle leaks expected when a parent terminates before a child?