quant
quant

Reputation: 23102

How to get a traceback from a depickled function

I am using dill to pickle a function and execute it on a remote environment, but when there is an exception on the remote instance, I can't seem to obtain a traceback. Here is a MRE:

# on the client:

import traceback
import dill

def foo():
    print('hello')
    raise Exception('exception')

def bar():
    try:
        foo()
    except Exception as e:
        print(traceback.format_exc())
        raise Exception(traceback.format_exc())

# pickle the function for execution in another kernel
with open('foo.pkl', 'wb') as f:
    dill.dump(bar, f, recurse=True)

If I just run bar() I get a helpful traceback as expected:

Exception: Traceback (most recent call last):
  File "/tmp/ipykernel_239196/3547136676.py", line 9, in bar
    foo()
  File "/tmp/ipykernel_239196/3547136676.py", line 5, in foo
    raise Exception('exception')
Exception: exception

Note the inclusion of the contents of the lines (ie. raise Exception('exception') and foo(). However, if I depickle the function in another kernel, the traceback module fails to produce a trace that shows me at which lines the exception happens:

# in a fresh kernel:
import dill
with open('foo.pkl', 'rb') as f:
    bar = dill.load(f)

bar()

This produces the following:

Exception: Traceback (most recent call last):
  File "/tmp/ipykernel_235171/3547136676.py", line 9, in bar
  File "/tmp/ipykernel_235171/3547136676.py", line 5, in foo
Exception: exception

Note that the issue is not that I'm trying to pickle the traceback, this is about the traceback itself not containing the information (ie. the content of the lines where the exception was raised).

How can I get this information on a remotely executed function when all I have is the pickled function object?

Upvotes: 0

Views: 18

Answers (0)

Related Questions