Index: uspace/srv/ns/task.c
===================================================================
--- uspace/srv/ns/task.c	(revision feeac0d8cd6e0ce736779bc5f1dc701c5cf42ba6)
+++ uspace/srv/ns/task.c	(revision c53b58e933d3da92e0e4a3461e7e13f4a3d81fb8)
@@ -40,14 +40,8 @@
 #include <macros.h>
 #include <malloc.h>
+#include <types/task.h>
 #include "task.h"
 #include "ns.h"
 
-
-/* TODO:
- *
- * As there is currently no convention that each task has to be waited
- * for, the NS can leak memory because of the zombie tasks.
- *
- */
 
 /** Task hash table item. */
@@ -195,5 +189,4 @@
 		}
 		
-		hash_table_remove(&task_hash_table, &pr->id);
 		list_remove(&pr->link);
 		free(pr);
@@ -204,8 +197,4 @@
 void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
 {
-	sysarg_t retval;
-	task_exit_t texit;
-	bool remove = false;
-	
 	ht_link_t *link = hash_table_find(&task_hash_table, &id);
 	hashed_task_t *ht = (link != NULL) ?
@@ -218,30 +207,24 @@
 	}
 	
-	if (!ht->finished) {
-		/* Add to pending list */
-		pending_wait_t *pr =
-		    (pending_wait_t *) malloc(sizeof(pending_wait_t));
-		if (!pr) {
-			retval = ENOMEM;
-			goto out;
-		}
-		
-		link_initialize(&pr->link);
-		pr->id = id;
-		pr->callid = callid;
-		list_append(&pr->link, &pending_wait);
+	if (ht->finished) {
+		task_exit_t texit = ht->have_rval ? TASK_EXIT_NORMAL :
+		    TASK_EXIT_UNEXPECTED;
+		ipc_answer_2(callid, EOK, texit, ht->retval);
 		return;
 	}
 	
-	remove = true;
-	retval = EOK;
-	
-out:
-	if (!(callid & IPC_CALLID_NOTIFICATION)) {
-		texit = ht->have_rval ? TASK_EXIT_NORMAL : TASK_EXIT_UNEXPECTED;
-		ipc_answer_2(callid, retval, texit, ht->retval);
-	}
-	if (remove)
-		hash_table_remove_item(&task_hash_table, link);
+	/* Add to pending list */
+	pending_wait_t *pr =
+	    (pending_wait_t *) malloc(sizeof(pending_wait_t));
+	if (!pr) {
+		if (!(callid & IPC_CALLID_NOTIFICATION))
+			ipc_answer_0(callid, ENOMEM);
+		return;
+	}
+	
+	link_initialize(&pr->link);
+	pr->id = id;
+	pr->callid = callid;
+	list_append(&pr->link, &pending_wait);
 }
 
@@ -314,4 +297,6 @@
 	ht->retval = IPC_GET_ARG1(*call);
 	
+	process_pending_wait();
+	
 	return EOK;
 }
@@ -336,4 +321,7 @@
 	ht->finished = true;
 	
+	process_pending_wait();
+	hash_table_remove(&task_hash_table, &id);
+	
 	return EOK;
 }
