diff --git a/C/threads.c b/C/threads.c
index f74249453..f0b2a0edf 100644
--- a/C/threads.c
+++ b/C/threads.c
@@ -294,7 +294,7 @@ Int
Yap_thread_detach_engine(int wid)
{
pthread_mutex_lock(&(ThreadHandle[wid].tlock));
- if (ThreadHandle[wid].handle == worker_id)
+ if (ThreadHandle[wid].handle == pthread_self())
ThreadHandle[wid].handle = 0;
ThreadHandle[wid].ref_count--;
pthread_mutex_unlock(&(ThreadHandle[wid].tlock));
diff --git a/changes-5.1.html b/changes-5.1.html
index 06419c9b9..828c6e35f 100644
--- a/changes-5.1.html
+++ b/changes-5.1.html
@@ -16,6 +16,7 @@
Yap-5.1.2:
+- FIXED: termination info for failed and detached threads (obs Paulo Moura).
- FIXED: check_callable should check for modules (obs Paulo Moura).
- NEW: add thread_sleep/1, mostly works like system:sleep/1 (request from Paulo Moura).
- NEW: add select/3 and intersection/3 to SWI emulation.
diff --git a/pl/threads.yap b/pl/threads.yap
index 5d42b78b7..b71fca184 100644
--- a/pl/threads.yap
+++ b/pl/threads.yap
@@ -35,11 +35,6 @@
'$current_module'(Module),
'$system_catch'((G,'$close_thread'(Detached,true) ; '$close_thread'(Detached,false)),Module,Exception,'$thread_exception'(Exception,Detached)).
-'$top_thread_goal'(_) :-
- '$thread_self'(Id0),
- recorda('$thread_exit_status', [Id0|false], _),
- '$run_at_thread_exit'(Id0).
-
'$close_thread'(Detached, Status) :-
'$thread_self'(Id0),
(Detached == true ->
@@ -209,7 +204,7 @@ thread_detach(Id) :-
thread_exit(Term) :-
'$thread_self'(Id0),
'$run_at_thread_exit'(Id0),
- recorda('$thread_exit_status', [Id0|Term], _),
+ recorda('$thread_exit_status', [Id0|exited(Term)], _),
'$thread_exit'.
'$run_at_thread_exit'(Id0) :-