Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/threads-ffi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Lerer <alerer@fb.com>2015-09-17 22:18:18 +0300
committerAdam Lerer <alerer@fb.com>2015-09-17 22:22:25 +0300
commit7e4409539c7ae07abb9c34fa76a56cae24af9209 (patch)
tree40d176b0d16cc95bb94dd0d73a26da3bc36ca922
parentd6b1b5aa3b5b3dcb10dd05ea58ded2b0946c854f (diff)
Capture the traceback in thread jobs
-rw-r--r--test/test-traceback.lua19
-rw-r--r--threads.lua15
2 files changed, 31 insertions, 3 deletions
diff --git a/test/test-traceback.lua b/test/test-traceback.lua
new file mode 100644
index 0000000..db50624
--- /dev/null
+++ b/test/test-traceback.lua
@@ -0,0 +1,19 @@
+local Threads = require 'threads'
+Threads.serialization('threads.sharedserialize')
+
+my_threads = Threads(1,
+ function()
+ -- nothing
+ end)
+
+my_threads:addjob(function()
+ function evil_func()
+ print('a'+1)
+ end
+ print("I'm doing fine")
+ evil_func()
+ end)
+
+ok, res = pcall(my_threads.synchronize, my_threads)
+assert(ok == false)
+assert(res:find("in function 'evil_func'"))
diff --git a/threads.lua b/threads.lua
index cc1ac6f..3bef540 100644
--- a/threads.lua
+++ b/threads.lua
@@ -1,6 +1,6 @@
local Queue = require 'threads.queue'
local clib = require 'libthreads'
-local unpack = unpack or table.unpack
+local _unpack = unpack or table.unpack
local Threads = {}
local Threads_ctor = {}
@@ -168,7 +168,9 @@ function Threads:dojob()
local endcallbacks = self.endcallbacks
local callstatus, args, endcallbackid, threadid = self.mainqueue:dojob()
if callstatus then
- local endcallstatus, msg = pcall(endcallbacks[endcallbackid], unpack(args))
+ local endcallstatus, msg = xpcall(
+ function() endcallbacks[endcallbackid](_unpack(args)) end,
+ debug.traceback)
if not endcallstatus then
table.insert(self.errors, string.format('[thread %d endcallback] %s', threadid, msg))
end
@@ -224,7 +226,14 @@ function Threads:addjob(...) -- endcallback is passed with returned values of ca
endcallbacks.n = endcallbacks.n + 1
local func = function(...)
- local res = {pcall(callback, ...)}
+ local args = {...}
+ local res = {
+ xpcall(
+ function()
+ local _unpack = unpack or table.unpack
+ callback(_unpack(args))
+ end,
+ debug.traceback)}
local status = table.remove(res, 1)
return status, res, endcallbackid
end