Skip to contentSkip to conversation

alchemy/task: Initialize thread handle on rt_task_bind

PATCH New Jan Kiszka 1 participant 2 messages Dec 19, 2025

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/2 alchemy/task: Initialize thread handle on rt_task_bind

From: Jan Kiszka <[email protected]>

This field was so far left alone, risking troubles if a function evaluating it using a stale value. That's so far rt_task_join, but we will soon use it also for additional integrity validation.

So, first initialize it to 0, on Linux an invalid pthread_t value. Then, if the task is a local one, pick up the ptid from the tcb so that even rt_task_join would start to work for process-local tasks.

---
 lib/alchemy/task.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
lib/alchemy/task.c +25 -5
index 42fca5a319..67f199ec24 100644
@@ -2160,11 +2160,31 @@ out:
 int rt_task_bind(RT_TASK *task,
 		 const char *name, RTIME timeout)
 {
-	return alchemy_bind_object(name,
-				   &alchemy_task_table,
-				   timeout,
-				   offsetof(struct alchemy_task, cobj),
-				   &task->handle);
+	struct alchemy_task *tcb;
+	struct service svc;
+	int ret;
+
+	task->thread = 0;
+
+	ret = alchemy_bind_object(name,
+				  &alchemy_task_table,
+				  timeout,
+				  offsetof(struct alchemy_task, cobj),
+				  &task->handle);
+	if (ret == 0) {
+		CANCEL_DEFER(svc);
+
+		tcb = get_alchemy_task(task, &ret);
+		if (tcb != NULL) {
+			if (threadobj_local_p(&tcb->thobj))
+				task->thread = tcb->thobj.ptid;
+			put_alchemy_task(tcb);
+		}
+
+		CANCEL_RESTORE(svc);
+	}
+
+	return ret;
 }
 
 /**
Signature · 1 line
2.51.0
author
#1 lore raw

PATCH 2/2 alchemy/task: Match thread handle on task lookup as well

From: Jan Kiszka <[email protected]>

This can catch stale task objects, namely when the original thread already terminated, its task handle was finalized and dropped from the registry but then reused by a new task object. The new one would normally get a new thread handle as well, allowing us to detect the stale of the original task object.

We just need to take into account the remote tasks or tasks currently in the process of being bound do not have a valid thread handle.

---
 lib/alchemy/task.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/alchemy/task.c +2 -1
index 67f199ec24..df5a6da76a 100644
@@ -104,7 +104,8 @@ static struct alchemy_task *find_alchemy_task(RT_TASK *task, int *err_r)
 	if (bad_pointer(tcb))
 		goto bad_handle;
 
-	if (threadobj_get_magic(&tcb->thobj) == task_magic)
+	if (threadobj_get_magic(&tcb->thobj) == task_magic &&
+	    (task->thread == tcb->thobj.ptid || task->thread == 0))
 		return tcb;
 bad_handle:
 	*err_r = -EINVAL;
Signature · 1 line
2.51.0