Skip to contentSkip to conversation

daemon: plug memory leak

PATCH In review René Scharfe 3 participants 6 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

v2 cover daemon: plug memory leak

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(-)
Signature · 1 line
2.6.2
#1 lore raw
Quoted 8 lines · René Scharfe

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

#2 lore raw
Quoted 6 lines · Jeff King
Quoted 4 lines · René Scharfe

Changes since v1:
- Rebased on next, which already has a cleanup function.
- Added first patch for renaming it.

Quoted 6 lines
Quoted 4 lines

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

Quoted 2 lines

-Peff

#3 lore raw

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

In reply to René Scharfe

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.

---
 run-command.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
run-command.c +6 -6
index 82cc238..b10ec75 100644
@@ -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) {
Signature · 1 line
2.6.2
#4 lore raw

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

In reply to René Scharfe

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

---
 Documentation/technical/api-run-command.txt | 7 +++++++
 run-command.c                               | 2 +-
 run-command.h                               | 1 +
 3 files changed, 9 insertions(+), 1 deletion(-)
Documentation/technical/api-run-command.txt +7 -0
index a9fdb45..8bf3e37 100644
@@ -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
run-command.c +1 -1
index b10ec75..0a3c24e 100644
@@ -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);
run-command.h +1 -0
index 9fe37ee..e296bd2 100644
@@ -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 *);
Signature · 1 line
2.6.2
#5 lore raw

PATCH v2 3/3 daemon: plug memory leak

In reply to René Scharfe

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).

---
 daemon.c | 1 +
 1 file changed, 1 insertion(+)
daemon.c +1 -0
index 56679a1..be70cd4 100644
@@ -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;
Signature · 1 line
2.6.2