Open apotterri opened 4 months ago
Instrument Pre-loaded Function References to Ensure Event Recording
Uninstrumented functions referenced at the module import stage do not generate any events in the recording. When application code saves a reference to a function upon importing, calls to this function fail to trigger events. This issue arises because instrumentation occurs after module load, affecting reference semantics.
When the program loads a module, any references to functions made before the instrumentation process will point to uninstrumented versions. Calls to these pre-loaded references will not generate events, as they bypass the instrumentation logic intended to track function calls.
To address this issue, it is necessary to ensure that function references saved at module import time are instrumented. One way to fix this is by wrapping references to functions at the point of assignment. This would involve modifying the function reference wrapping logic to check for already-referenced functions and instrument them as needed.
_appmap/instrument.py:
vendor/_appmap/wrapt/wrappers.py:
WeakFunctionProxy
and ObjectProxy
classes to ensure that references created before the instrumentation can be detected and instrumented._appmap/recorder.py:
_appmap/instrument.py:
instrument
function to handle pre-existing references.vendor/_appmap/wrapt/wrappers.py:
WeakFunctionProxy
class to ensure it can instrument references to functions pre-loaded before wrapping logic is applied.ObjectProxy
to accommodate instrumented pre-loaded references._appmap/recorder.py:
_add_event
method to include functionality for tracking events generated by calls through pre-loaded references.By implementing these changes, we ensure that calls to functions referenced before the instrumentation will generate the necessary events, maintaining the integrity of the recording process even for pre-loaded function references.
If application code saves a reference to function when the code is imported, a call to that function won't generate any events in the recording.
For example, with this definition
no events for the call to
free_method
will be recorded. This is because instrumentation offree_method
happens after the module is loaded, and so after the call topartial(free_method))
has saved the reference tofree_method
.