threads / patch / 40684

v2, 3 partsdaemon: plug memory leak

Subject: [PATCH v2 0/3] daemon: plug memory leak

## tl;dr

6 messages between Oct 31, 2015 and Oct 31, 2015. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

René Scharfe· Oct 31, 2015, 09:16 UTC · lore
Changes since v1:
- Rebased on next, which already has a cleanup function.
- Added first patch for renaming it.
Rene Scharfe (3):
   run-command: name the cleanup function child_process_clear()
   run-command: export child_process_clear()
   daemon: plug memory leak
  Documentation/technical/api-run-command.txt |  7 +++++++
  daemon.c                                    |  1 +
  run-command.c                               | 12 ++++++------
  run-command.h                               |  1 +
  4 files changed, 15 insertions(+), 6 deletions(-)
-- 
2.6.2
René Scharfe· Oct 31, 2015, 09:19 UTC · re: René Scharfe · lore

[PATCH v2 1/3] run-command: name the cleanup function child_process_clear()

Rename child_process_deinit() to child_process_clear() in order to stay consistent with similar cleanup functions like argv_array_clear(), string_list_clear() etc.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 run-command.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Show changes to run-command.c +6 −6
diff --git a/run-command.c b/run-command.c
index 82cc238..b10ec75 100644
--- a/run-command.c
+++ b/run-command.c
@@ -13,7 +13,7 @@ void child_process_init(struct child_process *child)
 	argv_array_init(&child->env_array);
 }
 
-static void child_process_deinit(struct child_process *child)
+static void child_process_clear(struct child_process *child)
 {
 	argv_array_clear(&child->args);
 	argv_array_clear(&child->env_array);
@@ -335,7 +335,7 @@ int start_command(struct child_process *cmd)
 fail_pipe:
 			error("cannot create %s pipe for %s: %s",
 				str, cmd->argv[0], strerror(failed_errno));
-			child_process_deinit(cmd);
+			child_process_clear(cmd);
 			errno = failed_errno;
 			return -1;
 		}
@@ -520,7 +520,7 @@ fail_pipe:
 			close_pair(fderr);
 		else if (cmd->err)
 			close(cmd->err);
-		child_process_deinit(cmd);
+		child_process_clear(cmd);
 		errno = failed_errno;
 		return -1;
 	}
@@ -546,7 +546,7 @@ fail_pipe:
 int finish_command(struct child_process *cmd)
 {
 	int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
-	child_process_deinit(cmd);
+	child_process_clear(cmd);
 	return ret;
 }
 
@@ -990,7 +990,7 @@ static void pp_cleanup(struct parallel_processes *pp)
 
 	for (i = 0; i < pp->max_processes; i++) {
 		strbuf_release(&pp->children[i].err);
-		child_process_deinit(&pp->children[i].process);
+		child_process_clear(&pp->children[i].process);
 	}
 
 	free(pp->children);
@@ -1157,7 +1157,7 @@ static int pp_collect_finished(struct parallel_processes *pp)
 		pp->nr_processes--;
 		pp->children[i].in_use = 0;
 		pp->pfd[i].fd = -1;
-		child_process_deinit(&pp->children[i].process);
+		child_process_clear(&pp->children[i].process);
 		child_process_init(&pp->children[i].process);
 
 		if (i != pp->output_owner) {
-- 
2.6.2
René Scharfe· Oct 31, 2015, 09:20 UTC · re: René Scharfe · lore

[PATCH v2 2/3] run-command: export child_process_clear()

Make the API symmetric by including a cleanup function as a counterpart to child_process_init().

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 Documentation/technical/api-run-command.txt | 7 +++++++
 run-command.c                               | 2 +-
 run-command.h                               | 1 +
 3 files changed, 9 insertions(+), 1 deletion(-)
Show changes to 3 files +9 −1

Documentation/technical/api-run-command.txt, run-command.c, run-command.h

diff --git a/Documentation/technical/api-run-command.txt b/Documentation/technical/api-run-command.txt
index a9fdb45..8bf3e37 100644
--- a/Documentation/technical/api-run-command.txt
+++ b/Documentation/technical/api-run-command.txt
@@ -46,6 +46,13 @@ Functions
 	The argument dir corresponds the member .dir. The argument env
 	corresponds to the member .env.
 
+`child_process_clear`::
+
+	Release the memory associated with the struct child_process.
+	Most users of the run-command API don't need to call this
+	function explicitly because `start_command` invokes it on
+	failure and `finish_command` calls it automatically already.
+
 The functions above do the following:
 
 . If a system call failed, errno is set and -1 is returned. A diagnostic
diff --git a/run-command.c b/run-command.c
index b10ec75..0a3c24e 100644
--- a/run-command.c
+++ b/run-command.c
@@ -13,7 +13,7 @@ void child_process_init(struct child_process *child)
 	argv_array_init(&child->env_array);
 }
 
-static void child_process_clear(struct child_process *child)
+void child_process_clear(struct child_process *child)
 {
 	argv_array_clear(&child->args);
 	argv_array_clear(&child->env_array);
diff --git a/run-command.h b/run-command.h
index 9fe37ee..e296bd2 100644
--- a/run-command.h
+++ b/run-command.h
@@ -47,6 +47,7 @@ struct child_process {
 
 #define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
 void child_process_init(struct child_process *);
+void child_process_clear(struct child_process *);
 
 int start_command(struct child_process *);
 int finish_command(struct child_process *);
-- 
2.6.2
René Scharfe· Oct 31, 2015, 09:20 UTC · re: René Scharfe · lore

[PATCH v2 3/3] daemon: plug memory leak

Call child_process_clear() when a child ends to release the memory allocated for its environment. This is necessary because unlike all other users of start_command() we don't call finish_command(), which would have taken care of that for us.

This leak was introduced by f063d38b (daemon: use cld->env_array when re-spawning).

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 daemon.c | 1 +
 1 file changed, 1 insertion(+)
Show changes to daemon.c +1 −0
diff --git a/daemon.c b/daemon.c
index 56679a1..be70cd4 100644
--- a/daemon.c
+++ b/daemon.c
@@ -802,6 +802,7 @@ static void check_dead_children(void)
 			/* remove the child */
 			*cradle = blanket->next;
 			live_children--;
+			child_process_clear(&blanket->cld);
 			free(blanket);
 		} else
 			cradle = &blanket->next;
-- 
2.6.2
Jeff King· Oct 31, 2015, 13:53 UTC · re: René Scharfe · lore

Re: [PATCH v2 0/3] daemon: plug memory leak

On Sat, Oct 31, 2015 at 10:16:57AM +0100, René Scharfe wrote:
Show 8 quoted lines
> Changes since v1:
> - Rebased on next, which already has a cleanup function.
> - Added first patch for renaming it.
> 
> Rene Scharfe (3):
>   run-command: name the cleanup function child_process_clear()
>   run-command: export child_process_clear()
>   daemon: plug memory leak
This round looks good to me. Thanks.
-Peff
Stefan Beller· Oct 31, 2015, 20:32 UTC · re: Jeff King · lore

Re: [PATCH v2 0/3] daemon: plug memory leak

On Sat, Oct 31, 2015 at 6:53 AM, Jeff King <peff@peff.net> wrote:
Show 12 quoted lines
> On Sat, Oct 31, 2015 at 10:16:57AM +0100, René Scharfe wrote:
>
>> Changes since v1:
>> - Rebased on next, which already has a cleanup function.
>> - Added first patch for renaming it.
>>
>> Rene Scharfe (3):
>>   run-command: name the cleanup function child_process_clear()
>>   run-command: export child_process_clear()
>>   daemon: plug memory leak
>
> This round looks good to me. Thanks.
Looks good to me, too

Thanks Stefan

>
> -Peff

← back to recent threads