OK well there's a short answer and a longer answer here. The short answer is that this is only a warning, not an "error" per se as it occurs during garbage collection, and is entirely harmless. To totally prevent them, probably ensuring that close() is called on all Session objects before removing all references to them would ensure that the identity map is completely separated from its contained InstanceState objects before Python's GC comes in and randomly disassociates things from each other, which is ultimately the cause of the error message.
Longer answer, line 196 of sqlalchemy/orm/identity.py is calling dict.__delitem__() on a key after checking the presence of that key, if this were changed to a dict.pop() this particular KeyError would disappear (probably a good idea, ticket 2267 http://www.sqlalchemy.org/trac/ticket/2267 is added). Basically all of your mapped objects have an attribute "._sa_instance_state" which refers to an InstanceState object, SQLAlchemy's master tracking structure for each of your object instances. InstanceState has a bidirectional relationship with the IdentityMap in which it's stored, IdentityMap is then an integral unit of its owning Session. InstanceState uses a weak referencing callback linked to your mapped object to remove itself from the IdentityMap when the target object is dereferenced completely, the remove operation happens via the dispose() method on IdentityMap. When Python garbage collects a whole field of objects, it's not deterministic what it will garbage collect first, and the GC operation is also in a different thread - so the job of InstanceState's weakref callback is greatly complicated by this - anything it does may fail as the gc thread, in conjunction with the actual user thread, may have changed the state of things in an indeterminate way. That's why errors which occur during async GC are inherently "ignored" by the Python interpreter - the operations are on objects that are already unreachable, and occur in a different thread so are already prone to random failures. On Aug 29, 2011, at 2:22 PM, Paul Andrew wrote: > Hi there, > > We're running load tests against our Pylons application, which uses > SQLAlchemy to hit an Oracle DB. > > Under load, we're starting to see errors pop up in our Apache logs: > > [Mon Aug 29 13:39:59 2011] [error] Exception KeyError: > KeyError((<class 'api.entity.proj.ent1.Ent1'>, (631,)),) in <bound > method InstanceState._cleanup of <sqlalchemy.orm.state.InstanceState > object at 0x2aaabca94d90>> ignored > [Mon Aug 29 13:39:59 2011] [error] Exception KeyError: > KeyError((<class 'api.entity.shared_entities.Ent'>, (5,)),) in <bound > method InstanceState._cleanup of <sqlalchemy.orm.state.InstanceState > object at 0x2aaab6ea9090>> ignored > [Mon Aug 29 13:39:59 2011] [error] Exception KeyError: > KeyError((<class 'api.entity.proj.ent2.Ent2>, (32,)),) in <bound > method InstanceState._cleanup of <sqlalchemy.orm.state.InstanceState > object at 0x2aaab7bf0410>> ignored > > > These errors only appear under load, I can't reproduce them by hitting > the same pages again manually. They also don't exactly correlate with > actual server errors; the requests that are happening at this time are > returning correctly. > > Is anyone able to tell me how to follow up on these kinds of errors, > so I can figure out what's causing them? > > Cheers, > > Paul > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalchemy@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.