Re: Pthread fd and memory leak



On Sep 12, 4:10 pm, Steve Watt <steve.removet...@xxxxxxxx> wrote:
In article <5f13d341-b633-4a6e-a34f-435f298f9...@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>,

CJ  <spamb...@xxxxxxxxxxxxxx> wrote:
I have created a pipe-line of threads to manage processing TCP/IP
connections.  It seems an fd (file descriptor of a socket) created in
one thread and closed in another is not returned to the process, and
not reused.

The first thread manages a listen socket passing the connected
(accept) sockets to another thread via an unnamed pipe, a worker
thread.  The worker thread "processes" the connection, shuts it down
(shutdown), and closes the socket file descriptor (close).  When the
server thread accepts the next connection the new fd is one greater
than the previous connection, a classic fd leak.

Wow, passing file descriptors over pipes between threads.  What a
strange concept.  Are you sure you're using threads?  If so, why
go to all the trouble?

When you send an fd over a pipe, the fd is still open in the sending
process.  The receiver will get a new fd in their own descriptor
space that also points to the same open file description.

To "correct" the leak, I created another unnamed pipe and the worker
thread sends the "processed" socket fd back for closing.  The server
reads from the pipe and closes it. This seems to "fix" the problem,
but raises the following question.

Which still shouldn't have fixed the problem.

Think of an fd pass through a pipe to another thread in the same
process as exactly equivalent to calling dup(fd).  Now you have
two fds to close.

Are fds shared by all threads of a process or unique to the thread
that "created" them?

Shared.

I am also finding that allocated heap memory created in one thread and
passed as a pointer value via an unnamed pipe to another thread has a
similar problem.  The receiving thread can access and manipulate the
memory at the pointer value and free it without errors, but the memory
is never reclaimed causing a memory leak.  Same question: is heap
memory shared by all threads within a process or unique to the thread
that allocates the memory?

Almost the same answer:  Shared.  However, there are some heap managers
that optimize for the case of free()s from the same thread that did
the allocation.  Or you could just be seeing a leak in your own code.

Any thoughts?  Any sites that provide specifics to what is and what is
not unique to a pthread would also be helpful.

Is there some reason for using pipes between threads?  It seems like
a strange design.
--
Steve Watt KD6GGD  PP-ASEL-IA          ICBM: 121W 56' 57.5" / 37N 20' 15.3"
 Internet: steve @ Watt.COM                      Whois: SW32-ARIN
   Free time?  There's no such thing.  It just comes in varying prices...

Yes there is a method to my madness and a reason for passing messages
"downstream" to processing threads. The reasons are similar to the
design of STREAMS, which I'll assume all the helpful posters grasp
already and will not explain.

Thanks for all the helpful posts. My (bone head) problem has been
found and turned out to be a memory error. Why the incorrectly
unreleased memory caused the problem (without any out-of-memory or
segv signals) is unknown.

cj
.



Relevant Pages

  • Re: Pthread fd and memory leak
    ... The first thread manages a listen socket passing the connected ... The worker thread "processes" the connection, ... and closes the socket file descriptor. ... When you send an fd over a pipe, the fd is still open in the sending ...
    (comp.programming.threads)
  • Re: Can a process start threads with individual user rights?
    ... individual pages of memory are physically shared until one of the ... processes changes them. ... a pipe is simpler than a socket and suffices for this purpose. ...
    (comp.programming.threads)
  • BIND 9.4-ESVb1 is now available.
    ... BIND 9.4-ESVb1 is a extended release version beta for BIND 9.4. ... API and glibc hides parts of the IPv6 Advanced Socket ... WARNING: API CHANGE: over memory callback ... If allow-recursion is not set in named.conf then ...
    (comp.protocols.dns.bind)
  • Robust shared memory for unrelated processes
    ... Suppose for simplicity's sake that one wishes to share a sizable piece of memory with a single other process with which one is already in contact ... If I am SIGKILL'd at any time between creating the section and receiving confirmation that my partner has opened it, the section will persist until reboot. ... The situation is as bad as with SysV SHM. ... Suppose now I get a bit cleverer; I use POSIX SHM, but I create and then immediately unlink my section, before sending the file descriptor over a Unix domain socket to my partner. ...
    (Linux-Kernel)
  • Re: Optiplex GX300 Memory
    ... sockets, and the second socket seems to have a dummy module. ... with unused memory sockets will have them. ... P3 RDRAM systems with the 820 chipset can handle two different ... mismatched RAMBUS modules. ...
    (alt.sys.pc-clone.dell)

Loading