For some reason lesson_timer, lesson_branch and lesson_attempts have no related id and hence no relationship. When deleting a full attempt the code sorts by starttime in lesson_timer, and almost randomly, removes one. Before
MDL-37250 it would leave orphaned records in the lesson_timer table.
One way to solve this issue is to add fields to lesson_branch and lesson_attempts that link it back to lesson_timer so that all the appropriate records can be removed. The reason it should use the lesson_time id is because this table actually keeps a record of each attempt regardless of whether the lesson is a timed lesson or not.