pprofile icon indicating copy to clipboard operation
pprofile copied to clipboard

Exception ignored: NoneType has no attribute f_code

Open akiross opened this issue 5 years ago • 6 comments

Hello, thanks for pprofile! I got this exception while trying to run a script, and apparently this is being raised just before (or at the beginning) of my script. Also, it is not deterministic: the same script launched with the same inputs will not raise this exception every time.

Exception ignored in: <function WeakSet.__init__.<locals>._remove at 0x7f844517aea0>                                                                                                                                                                          
Traceback (most recent call last):                                                                                                                                                                                                                            
  File "/usr/local/lib/python3.7/_weakrefset.py", line 38, in _remove                                                                                                                                                                                         
    def _remove(item, selfref=ref(self)):                                                                                                                                                                                                                     
  File "/usr/local/lib/python3.7/site-packages/pprofile.py", line 916, in _real_global_trace                                   
    callee_dict[(frame.f_back.f_code, frame.f_code)].append(callee_entry)                                                                                                                                                                                     
AttributeError: 'NoneType' object has no attribute 'f_code'

I launched with pprofile --output /path/to/out.txt myscript.py, if it is of any interest. Did I do something wrong or is this a bug? In the latter case, please let me know if I can do something more to help.

akiross avatar Jun 20 '19 11:06 akiross

Hello,

This means frame.f_back is None (frame is topmost), but call stack is not empty (so there is a known caller). The error traceback is consistent with no caller existing (_remove is being called from nowhere, maybe hidden gc magic ?). Or some threading magic (but I doubt it) ?

I suspect the bug is in pprofile, but to properly fix it I will need your help understanding why it triggers. As a first step you can add an if frame.f_back is not None: in _real_global_trace's else: block so it does not execute if there is no caller frame.

vpelletier avatar Jun 22 '19 08:06 vpelletier

Hello, I am sorry, but I am unable to help on this matter anymore: changed job, I no longer have the codebase where this happened.

akiross avatar Jun 30 '19 10:06 akiross

as a headsup - I got exactly the same error running python -m trace -t -g -c -C 'path/to/coveragedir' so it looks more fundamental than your library

flipdazed avatar Jan 09 '20 11:01 flipdazed

Also came across this issue with a similar setup, any ideas on how to fix this?

Traceback (most recent call last):
  File "/usr/lib/python3.8/_weakrefset.py", line 38, in _remove
    def _remove(item, selfref=ref(self)):
  File "/usr/local/lib/python3.8/dist-packages/pprofile/__init__.py", line 1042, in _real_global_trace
    def _remove(item, selfref=ref(self)):
  File "/usr/local/lib/python3.8/dist-packages/pprofile/__init__.py", line 1042, in _real_global_trace
        callee_dict[(frame.f_back.f_code, frame.f_code)].append(callee_entry)def _remove(item, selfref=ref(self)):

  File "/usr/local/lib/python3.8/dist-packages/pprofile/__init__.py", line 1042, in _real_global_trace
AttributeError:     callee_dict[(frame.f_back.f_code, frame.f_code)].append(callee_entry)
AttributeError:     'NoneType' object has no attribute 'f_code''NoneType' object has no attribute 'f_code'

stlucas44 avatar Oct 05 '23 06:10 stlucas44

Aha !

Does the code being profiled (or its dependencies) use generators, including coroutines ?

This does not seem to be enough on its own, so something else is needed to trigger this issue. So far I do not see what, and I must say that I have never dug into coroutines (...and I suspect the caller/callee notion just melts in such context), although I am familiar with garden-variety generators.

vpelletier avatar Oct 06 '23 11:10 vpelletier

In my case a ROS node is profiled, involving msg passing and service calls. So its highly probable that coroutines are involved. However my knowledge on coroutines and generators is fairly shallow.

Thanks for responding btw!

stlucas44 avatar Oct 06 '23 13:10 stlucas44