From: Pat Thoyts <patthoyts@users.sourceforge.net>
To: kusmabite@gmail.com
Cc: git@vger.kernel.org, gitster@pobox.com
Subject: Re: [PATCH v6 00/16] daemon-win32
Date: Thu, 04 Nov 2010 01:04:52 +0000 [thread overview]
Message-ID: <87r5f1x5jf.fsf@fox.patthoyts.tk> (raw)
In-Reply-To: <AANLkTimcYdQD-En+SfkH5dkaxaWXveuA5Oz-Hn0Cf1v+@mail.gmail.com> (Erik Faye-Lund's message of "Thu, 4 Nov 2010 01:53:57 +0100")
Erik Faye-Lund <kusmabite@gmail.com> writes:
>On Thu, Nov 4, 2010 at 1:28 AM, Pat Thoyts
><patthoyts@users.sourceforge.net> wrote:
>> Erik Faye-Lund <kusmabite@gmail.com> writes:
>>>On Wed, Nov 3, 2010 at 11:18 PM, Erik Faye-Lund <kusmabite@gmail.com> wrote:
>>>> diff --git a/compat/mingw.c b/compat/mingw.c
>>>> index b780200..47e7d26 100644
>>>> --- a/compat/mingw.c
>>>> +++ b/compat/mingw.c
>>>> @@ -1519,8 +1519,10 @@ pid_t waitpid(pid_t pid, int *status, unsigned options)
>>>> }
>>>>
>>>> if (pid > 0 && options & WNOHANG) {
>>>> - if (WAIT_OBJECT_0 != WaitForSingleObject((HANDLE)pid, 0))
>>>> + if (WAIT_OBJECT_0 != WaitForSingleObject((HANDLE)pid, 0)) {
>>>
>>>AAAND the last one is right here as well:
>>>- if (WAIT_OBJECT_0 != WaitForSingleObject((HANDLE)pid, 0))
>>>+ if (WAIT_OBJECT_0 != WaitForSingleObject(h, 0)) {
>>>
>>
>> Applying both of these doesn't fix the handle leak when I test
>> this. Looking further I believe it is due to the use of a reallocated
>> array. When you remove a pinfo structure you realloc to the one you
>> found, potentially freeing items you still require.
>>
>
>If this is the reason, then that's a bug. The code TRIES to do the
>right thing by memmove'ing the the entro to be deleted to the end.
>Looking over the code, I still don't see anyhing obviously wrong.
>But...
>
>> Attached is a patch that switches to a linked list for this
>> instead. Using this I no longer accumulate leaked handles.
>>
>> ----
>> From e0c05f8f9ed8729648eea92cf654f357fa884e40 Mon Sep 17 00:00:00 2001
>> From: Pat Thoyts <patthoyts@users.sourceforge.net>
>> Date: Thu, 4 Nov 2010 00:23:08 +0000
>> Subject: [PATCH] win32-daemon: fix handle leaks
>>
>> The use of an array of pinfo structures and the realloc used when cleaning
>> up a closed child can free structures that are still in use. Use a linked
>> list instead.
>> This stops the leaking of handles in the win32-daemon.
>>
>> Signed-off-by: Pat Thoyts <patthoyts@users.sourceforge.net>
>> ---
>> compat/mingw.c | 43 ++++++++++++++++++++++++-------------------
>> 1 files changed, 24 insertions(+), 19 deletions(-)
>>
>> diff --git a/compat/mingw.c b/compat/mingw.c
>> index b780200..29f4036 100644
>> --- a/compat/mingw.c
>> +++ b/compat/mingw.c
>> @@ -637,11 +637,12 @@ static int env_compare(const void *a, const void *b)
>> return strcasecmp(*ea, *eb);
>> }
>>
>> -struct {
>> +struct pinfo_t {
>> + struct pinfo_t *next;
>> pid_t pid;
>> HANDLE proc;
>> -} *pinfo;
>> -static int num_pinfo;
>> +} pinfo_t;
>> +struct pinfo_t *pinfo = NULL;
>> CRITICAL_SECTION pinfo_cs;
>>
>> static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
>> @@ -746,10 +747,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
>> * Keep the handle in a list for waitpid.
>> */
>> EnterCriticalSection(&pinfo_cs);
>> - num_pinfo++;
>> - pinfo = xrealloc(pinfo, sizeof(*pinfo) * num_pinfo);
>> - pinfo[num_pinfo - 1].pid = pi.dwProcessId;
>> - pinfo[num_pinfo - 1].proc = pi.hProcess;
>> + {
>> + struct pinfo_t *info = xmalloc(sizeof(struct pinfo_t));
>> + info->pid = pi.dwProcessId;
>> + info->proc = pi.hProcess;
>> + info->next = pinfo;
>> + pinfo = info;
>> + }
>> LeaveCriticalSection(&pinfo_cs);
>>
>> return (pid_t)pi.dwProcessId;
>> @@ -1519,13 +1523,15 @@ pid_t waitpid(pid_t pid, int *status, unsigned options)
>> }
>>
>> if (pid > 0 && options & WNOHANG) {
>> - if (WAIT_OBJECT_0 != WaitForSingleObject((HANDLE)pid, 0))
>> + if (WAIT_OBJECT_0 != WaitForSingleObject(h, 0)) {
>> + CloseHandle(h);
>> return 0;
>> + }
>> options &= ~WNOHANG;
>> }
>>
>> if (options == 0) {
>> - int i;
>> + struct pinfo_t **ppinfo;
>> if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
>> CloseHandle(h);
>> return 0;
>> @@ -1536,17 +1542,16 @@ pid_t waitpid(pid_t pid, int *status, unsigned options)
>>
>> EnterCriticalSection(&pinfo_cs);
>>
>> - for (i = 0; i < num_pinfo; ++i)
>> - if (pinfo[i].pid == pid)
>> + ppinfo = &pinfo;
>> + while (*ppinfo) {
>> + struct pinfo_t *info = *ppinfo;
>> + if (info->pid == pid) {
>> + CloseHandle(info->proc);
>> + *ppinfo = info->next;
>> + free(info);
>> break;
>> -
>> - if (i < num_pinfo) {
>> - CloseHandle(pinfo[i].proc);
>> - memmove(pinfo + i, pinfo + i + 1,
>> - sizeof(*pinfo) * (num_pinfo - i - 1));
>> - num_pinfo--;
>> - pinfo = xrealloc(pinfo,
>> - sizeof(*pinfo) * num_pinfo);
>> + }
>> + ppinfo = &info->next;
>> }
>>
>> LeaveCriticalSection(&pinfo_cs);
>
>...yeah, using a linked list is more elegant. Do you mind if I snatch
>your code and leave a comment in the commit message?
>
Sure. When I wrote the comment I'd forgotten about the memmove. So
hmmm. However, I just pulled you win32-daemon branch and applied this
patch on top. Now there are no more warnings so your union fix is
good. As I understand it thats the way to handle this particular
warning. ipv6 and ipv4 both still work fine as well.
With my patch a while true; do git ls-remote; done shows no more leaking
handles or memory. So the use of a list is fixing something. Looking
great now.
--
Pat Thoyts http://www.patthoyts.tk/
PGP fingerprint 2C 6E 98 07 2C 59 C8 97 10 CE 11 E6 04 E0 B9 DD
next prev parent reply other threads:[~2010-11-04 1:05 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-03 16:31 [PATCH v6 00/16] daemon-win32 Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 01/16] mingw: add network-wrappers for daemon Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 02/16] mingw: implement syslog Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 03/16] compat: add inet_pton and inet_ntop prototypes Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 04/16] inet_ntop: fix a couple of old-style decls Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 05/16] mingw: use real pid Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 06/16] mingw: support waitpid with pid > 0 and WNOHANG Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 07/16] mingw: add kill emulation Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 08/16] daemon: use run-command api for async serving Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 09/16] daemon: use full buffered mode for stderr Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 10/16] Improve the mingw getaddrinfo stub to handle more use cases Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 11/16] daemon: get remote host address from root-process Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 12/16] mingw: import poll-emulation from gnulib Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 13/16] mingw: use " Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 14/16] daemon: use socklen_t Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 15/16] daemon: make --inetd and --detach incompatible Erik Faye-Lund
2010-11-03 16:31 ` [PATCH v6 16/16] daemon: opt-out on features that require posix Erik Faye-Lund
2010-11-03 21:11 ` [PATCH v6 00/16] daemon-win32 Pat Thoyts
2010-11-03 22:18 ` Erik Faye-Lund
2010-11-03 22:39 ` Erik Faye-Lund
2010-11-04 0:28 ` Pat Thoyts
2010-11-04 0:53 ` Erik Faye-Lund
2010-11-04 1:04 ` Pat Thoyts [this message]
2010-11-03 22:58 ` Erik Faye-Lund
2010-11-04 0:06 ` Erik Faye-Lund
2010-11-04 0:28 ` Erik Faye-Lund
2010-11-04 8:58 ` Martin Storsjö
2010-11-04 9:15 ` Erik Faye-Lund
2010-11-04 9:35 ` Martin Storsjö
2010-11-04 10:15 ` Erik Faye-Lund
2010-11-04 8:52 ` Martin Storsjö
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87r5f1x5jf.fsf@fox.patthoyts.tk \
--to=patthoyts@users.sourceforge.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=kusmabite@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.