Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture exceptions when context no longer exists #81

Closed
wants to merge 1 commit into from

Conversation

polothy
Copy link

@polothy polothy commented Dec 19, 2017

This was actually discovered in an unrelated unit test (the course was deleted in the test), but seems like a good change since this subscribes to all events and could likely happen elsewhere.

@FMCorz
Copy link
Owner

FMCorz commented Dec 20, 2017

Interesting. In what circumstances is Moodle broadcasting events that are no longer attached to a context? I wasn't aware that this had to be catered for. Is it documented anywhere? Thanks!

@polothy
Copy link
Author

polothy commented Dec 20, 2017

The unit test runs in a transaction. While a transaction is open, it appears that events are buffered. Durning the open transaction, the course is created and deleted. After the test is done, the transaction is closed and the buffered events are sent.

So, yes, unlikely to run into this in Moodle standard, but could happen. I patched it on our end, take it if you like or not. Just wanted to share the patch.

Is it documented anywhere?

Hah, good one 😉

@FMCorz
Copy link
Owner

FMCorz commented Dec 21, 2017

Thanks for the details. It's interesting that the try/catch works because get_context() would return false, not throw an exception. Ultimately, because of false possibility, all code should always handle that possibility, it just feels very redundant.

I guess if the observer was internal, then this wouldn't happen as it would be notified as soon as the event occurs, not after the transaction. Which is fine, but seems unnecessary if the transaction is rolledback.

Thanks for spotting it!

@FMCorz FMCorz added the bug label Dec 21, 2017
@polothy
Copy link
Author

polothy commented Dec 27, 2017

Thanks for the details. It's interesting that the try/catch works because get_context() would return false, not throw an exception. Ultimately, because of false possibility, all code should always handle that possibility, it just feels very redundant.

It's because get_context actually returns the cached context object and it is actually has_capability that throws it because it tries to query for parent contexts.

An alternative fix would be to put the try/catch into your main entry point for all the observed events. Being unfamiliar with the code, wasn't sure if that was a good fix.

@FMCorz
Copy link
Owner

FMCorz commented Mar 20, 2018

So I tried to reproduce this in a testcase but didn't succeed. Transactions are handled differently depending on the database engine, etc... so I gave up writing a testcase for it. However I wrote a commit which a) checks that get_context returns something, and b) captures the exception as you suggested.

Thanks! Now back to GDPR...

@FMCorz FMCorz closed this in 28c3f94 Mar 20, 2018
@polothy
Copy link
Author

polothy commented Mar 20, 2018

Thanks

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

Successfully merging this pull request may close these issues.

2 participants