-
Notifications
You must be signed in to change notification settings - Fork 204
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
DetachedInstanceError when harvesting CKAN #151
Comments
Also getting this error constantly, though my CKAN instance version is My config settings are:
And a local group catalogue is present. |
You probably know this, but just in case: when you get an ORM object (e.g. with model.Package.get(id) or model.Session.query(model.Package).first()) then you can use it until you do a model.Session.commit()/close()/remove(). After that you get a DetachedInstanceError if you try and use the ORM object again. That is the most likely cause. I suggest you put in a breakpoint or use prints to find out where this is happening and fix it. |
Seems like adding |
Dave, thanks for the info - didn't know that. As I was using a vanilla ckan Raphael, thanks I'll try that out as soon as I can, gonna be early next
|
Please everyone, let's help each other more - if no-one tries to fix Deferring the commit sounds like a bad solution that complicates things. It On 8 September 2015 at 12:17, Florian Mayer [email protected]
|
Nope, not deferring, only on holidays this week and banned from coding by
|
Wait I think I misunderstood what Dave meant with "deferring". Nevertheless
|
Sorry Florian, I overreacted. I just want to guard against people thinking On 8 September 2015 at 12:46, Florian Mayer [email protected]
|
BTW I've just added a testing framework for harvesters, which might help you identify the problem, if it is to do with your specific harvester. And if you can write a failing test it helps other people reproduce and help with the problem. See this example for the CKAN harvester: |
@davidread thanks for the testing framework, I'll give it a whirl! FYI my particular harvesting requirements ended up being highly customised and co-evolving with the harvesting process, so I scripted the process in the first instance, here's the code for reference. I'm breaking away from the ckanext-harvesting paradigm in two incompatible ways:
|
Interesting point about ckanext-scheming and harvesting - I've not used scheming yet but I guess it means adapting whatever harvester for that. The WMS/WFS thing looks annoying to deal with. I quickly looked at your 'harvest' scripts and I guess you could simply drop them into a harvester gather_stage and they'd run fine, upsert and all. Then the harvest framework would give you a web front-end for viewing and running them, could schedule runs etc. but you'd lose having them in a python notebook which is easier to toy with when necessary. (The harvest framework assumes you'd create datasets across a single organization, but if you hid or ignored that field I don't think there'd be a problem.) |
Encountered the same error. My CKAN is version 2.4.0 running on an ubuntu, not running any customisations. Strangely, harvesting worked for several months, until just now. I'll list what I did, in case it might help find the origin problem. I'm not a Python developer, so I can't trace this down myself.
Below is the error stacktrace. This error occured when I started harvesting, and now occurs whenever I try to start the fetch_consumer script.
Note that the invalid resource error is probably related to one of my harvesters using an incorrect DCAT feed. |
When going down the rabbit hole to fix or better said avoid this error, I noticed when disabling the core CKAN |
How exactly do you disable/replace that plugin? Further update: I also noticed that the |
Simply by modifiying the plugins list (i.e. ckan.plugins) in |
Final update: I have a situation where I can recreate the error. It is related to the dcat file (my dcat file is missing some necessairy attributes), but I can't narrow it down. I managed to find a single dataset entry in the DCAT that crashed it, only to work it when I reinitialized the VM. When starting from a completely fresh CKAN installation, I can't reproduce the error, so I guess it has to do with the data that is already present in the DB. However, I'm not sure how to further investigate this error. My Python-fu is limited, and I have no idea how to debug something that interacts between 3 python processes. If someone gives me some pointers (within reasonable time of this post), I can gather more data. |
@DieterDePaepe Would you providing the DCAT file? Either with a link or putting it in a pastebin, so I can try creating a test? The stacktrace shows the problem occurs when the harvester's import stage, when tries to package_show the pre-existing dataset and that tries to access c.userobj. Now c.userobj is initialized in BaseController, but I'm struggling to see how that would have been run by a harvester. I can't get it to initialize running the existing DCAT harvester tests. BTW did disabling the recline_view plugin make the error go away? |
@DieterDePaepe You could also try to see what is initializing c.userobj, by inserting this breakpoint into ckan/ckan/lib/base.py around line 281:
and then run the fetch_consumer. Hopefully it should give you a prompt and you can type |
DCAT feed that causes crash for me can be found at http://pastebin.com/9j9amRxg As I mentioned before I tried to narrow down the DCAT to produce the same error. I ended up with the 2nd listed dataset (about crime). But since this smaller DCAT didn't cause an error when I tried again, I'm not sure it's strictly related to that one. Haven't tried yet to disable the recline_view plugin. Will try that and the debug command soon. |
I tried disabling the recline_view plugin and tried replacing it with recline_grid_view. After restarting apache and reloading the harvesters, the error was still present. Adding the python debugger: after adding it, the ckan website gives an internal error, the logs mention a BdbQuit error, which according to google occurs when the debugger cannot get i/o for interaction. So that makes sense and shouldn't interfere with the debugging of the harvester. However, when running the run command, I get the same DetachedInstanceError as before, no debugger. Perhaps this is because I am running a paster command? The commands I use to run harvesting are (each in a different console that I keep open):
|
Any update on this issue? We start to see this errors on one of our servers (it's actually on our test server, fortunately the production works). But they use the same data and have the same configuration. The |
Does this give any clues? ckan/ckan#1714 (comment) Sorry, it's a long read but very thorough and it fixed a similar problem I had once with spatial harvesters, so might be worth checking. |
@amercader after an extended debugging session, we were finally able to pin down this issue. It was a very unfortunate chain of circumstances that led to this error: TL;DR:
Long story for further reference:Our custom harvester has the requirement to not update the package metadata, but only updated the ressources (because it's possible that the package metadata is changed manually after the harvester runs). Therefore we make calls to This failed for most of our packages, i.e. we got in the except block with a KeyError for the key 'resources'. After some looking around, we found the code responsible to this KeyError: try:
context['defer_commit'] = True
context['use_cache'] = False
_get_action('package_update')(context, pkg_dict)
context.pop('defer_commit')
except ValidationError, e:
errors = e.error_dict['resources'][-1]
raise ValidationError(errors) Here comes the interesting part for the DetachedInstanceError. Because we were guilty of not create a new context for each of our calls, this code changed our context. i.e. To add more confusion, the raised exception does not contain a SolutionWe ended up adding a little script to "migrate" the old packages to get rid of the extras. This leads to the situation, that we don't get a ValidationError anymore, and therefore everything runs smoothly. The problem, that the code in I just wanted to leave this here, in case someone needs some more hints to debug this. |
@metaodi Really glad to hear that you were successful after what it sounds like an intense debugging session :) Legacy harvesters might have partial blame, but the |
I just installed the new release of CKAN (2.7.3) and encountered same error
I followed the whole process of installing ckan, datastore, datapusher and harvester. |
When I had to migrate data into CKAN, I gave up on the harvester, and used the CKAN API from an iPython notebook: https://github.com/datawagovau/harvesters (same link as in my comment above) Harvesters are great when running as a scheduled task, but introduce one extra level of complexity which can conflict with other customisations (e.g. ckanext-scheming overriding "extra" fields). Simply whacking the API with a wrench until the data goes in always works. |
@MaciejJanowski you can discount datapusher and datastore. The problem you're seeing is with ckanharvester. A bit about your context might help us think about this:
|
|
@MaciejJanowski thanks for this. All sounds standard enough. The tests pass, so I I guess the next step is for you or another developer to investigate why it happens and/or reproduce it in a test. You could use print statements or pdb to find out when the User object is instantiated, when its tried to be used and it's found to be detached, and then track down when it gets detached. At least you're seeing it happen every time, which helps(!) |
I'm hitting the same problem while using the CSW harvester, using
FWIW, here's what I observe and how I fixed it, although I'm far from understanding what's exactly happening. The root cause is a harvested package that fails to validate, causing an error and a rollback of the session (see https://github.com/ckan/ckan/blob/e838541d/ckan/logic/action/create.py#L175). I fixed it by checking if the userobj is still attached :
That's not a satisfying fix; i'd rather understand why this userobj gets detached in the first place or whether it should be re-attached, but that's as far as I can go. |
When harvesting a CKAN instance (source install v2.5.0a as of July 2015) into another CKAN instance (datacats using latest master, v2.5.0a) with a custom ckanext-scheming dataset schema, I'm running into DetachedInstanceError indicating that the db conection via SQLAlchemy changes sessions.
My config:
I've created a sysadmin user called "harvest", same error as with my own sysadmin account.
I haven't seen a DetachedInstanceError since mentioned in the dev meeting archives a year ago.
Has anyone seen this error lately, or is harvesting only broken on my end?
The text was updated successfully, but these errors were encountered: