vpelletier / pprofile

Line-granularity, thread-aware deterministic and statistic pure-python profiler
GNU General Public License v2.0
447 stars 28 forks source link

Exception ignored: NoneType has no attribute f_code #30

Open akiross opened 5 years ago

akiross commented 5 years ago

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.

vpelletier commented 5 years ago

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.

akiross commented 5 years ago

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.

flipdazed commented 4 years ago

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

stlucas44 commented 1 year ago

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'
vpelletier commented 1 year ago

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.

stlucas44 commented 1 year ago

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!