We had an issue where a module did not clean up its grades when deleted (turnitintool to name and shame). This leaves a load of records in the mdl_grade_items table relating to a module that doesn't exist.
It would have saved some debugging if the get_coursemodule_from_instance() function made a check that the requested modulename is actually installed on the system. If a modulename is given to this function that is not installed (for whatever reason) it attempts to do a db join on the module table which doesn't exist. It seems prudent (to me) that this check should be made.