Problem was that the code didn't handle a transaction created in innodb as part of a failed mysql_lock_tables()