Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Keeping references to event callbacks after unsubscribe_event #185

Closed
reszelaz opened this issue Feb 16, 2018 · 4 comments
Closed

Keeping references to event callbacks after unsubscribe_event #185

reszelaz opened this issue Feb 16, 2018 · 4 comments

Comments

@reszelaz
Copy link
Contributor

Hi PyTango experts!

We have observed that the reference to the callback (either method or function) is being maintained after unsubscribing from the event. We have checked it for configuration and change event.

After executing this code:

import gc
import tango

def fcb(*args, **kwargs):
    pass

dp = tango.DeviceProxy("sys/tg_test/1")
print "Before sub:", len(gc.get_referrers(fcb))
_id = dp.subscribe_event("double_scalar", tango.EventType.CHANGE_EVENT, fcb)
print "After sub: ", len(gc.get_referrers(fcb))
dp.unsubscribe_event(_id)
gc.collect()
print "After unsub:", len(gc.get_referrers(fcb))

we can see the following output:

Before sub: 1
After sub:  2
After unsub: 2

Note that this does not happen with PyTango 7.2.6 but with PyTango 8.1.2 and PyTango 9.2.2.

Could you confirm that this is a bug?

Thanks!

@vxgmichel
Copy link
Contributor

vxgmichel commented Feb 20, 2018

Hi @reszelaz and thanks for the report!

After some investigation it turns out the device proxy keeps the callback in memory for at least a minute after the call to unsubscribe_event. This seems to be related to sourceforge issue #659. The reference to the callback is freed during the following unsubscribe_event. You can verify this behavior using the following code:

import gc
import sys
import time
import tango

def callback(*args):
    print(args)

# Run
dp = tango.DeviceProxy("sys/tg_test/1")
eid = dp.subscribe_event('time', tango.EventType.CHANGE_EVENT, callback)
dp.unsubscribe_event(eid)

# Cleanup
time.sleep(61)
try:
    dp.unsubscribe_event(eid)
except KeyError:
    sys.exc_clear()

# Check
assert gc.get_referrers(callback) == [globals()]

@reszelaz
Copy link
Contributor Author

Thanks for the investigation! The current behavior seems to be a little "fragile". Do you plan to change it?

Actually we found this when working on two issues in Taurus: taurus-org/taurus#511 and taurus-org/taurus#512. @cmft and @cpascual figured out a workaround that is already integrated in Taurus so there is no big rush with this discussion. When we will get a breath after the Taurus and Sardana releases we will get back to you.

@vxgmichel
Copy link
Contributor

The current behavior seems to be a little "fragile".

It definitely is. There are several ways to tackle this issue:

  • get rid of the fix and see if issue #659 keeps happening (it might have been fixed in libtango)
  • investigate and see if a proper memory management is doable on the cpp side
  • lower the the unsubscribe lifetime (1 second should be more than enough)

@reszelaz
Copy link
Contributor Author

Great! If you need any help in testing any of this solutions we are willing to help!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants