Introduction: The Worst Bug in a CBT Platform
Part 5 was about people trying to break in. This time I want to talk about something that, in my view, hurts a school more than most security bugs: a student finishes an exam and the result is wrong, missing, or recorded twice.
The funny thing is, none of the problems below needed an attacker. All they needed was a student with a slow connection, a double click, a closed laptop lid, or a teacher who allowed retakes. I found them by reading ExamRunnerService line by line and asking myself one question for every method: what happens if this runs twice at the same time, or fails halfway?
I shipped the fixes in Pull Request #25: feat/roadmap-hardening, together with the scaling work I cover in Part 7.
Click the diagram to open it at full size.
1. What Was Wrong
| # | Problem | What the student or teacher saw |
|---|---|---|
| 1 | Latest attempt loaded with a query that expects one row | Retake allowed → second attempt → every later request returned 500 |
| 2 | Time-expired attempt graded, then an exception rolled the grade back | Attempt stuck in_progress forever, no score |
| 3 | Attempts were only finalized when the student came back | Student closes the tab → no result until they log in again |
| 4 | Two "start" requests at once | Two attempts with the same number |
| 5 | Submit and a timeout path finishing the same attempt | Graded twice, two EXAM_SUBMITTED events |
| 6 | Short answers ignored by the grader; one graded essay marked the whole attempt completed | Wrong scores, unfinished grading shown as final |
2. The Retake Crash: One Row Expected, Two Found
My repository had a derived query that returns Optional:
// Before PR #25
Optional<ExamAttempt> findByExaminationIdAndStudentId(Long examinationId, Long studentId);
That's correct only while each student has a single attempt. As soon as a teacher enabled allowRetry and a student started a second attempt, Spring Data found two rows for an Optional return type and threw IncorrectResultSizeDataAccessException. From then on, the student got a 500 on every request to that exam.
The fix was to ask for exactly what I wanted, the latest attempt:
/** The student's most recent attempt (exams with retries can have several). */
Optional<ExamAttempt> findFirstByExaminationIdAndStudentIdOrderByAttemptNumberDesc(Long examinationId, Long studentId);
The same method hid a second, quieter bug. It treated only completed and submitted as finished, so an attempt in needs_grading or force_finished got "resumed": the student landed back in an exam they could no longer answer. Now I count all four states as finished:
/** Attempt states after which no more answers are accepted. */
private static final Set<AttemptStatus> FINISHED_STATUSES = EnumSet.of(
AttemptStatus.completed, AttemptStatus.submitted, AttemptStatus.needs_grading, AttemptStatus.force_finished);
Optional<ExamAttempt> latestAttemptOpt = attemptRepository
.findFirstByExaminationIdAndStudentIdOrderByAttemptNumberDesc(examId, student.getId());
if (latestAttemptOpt.isEmpty()) {
attempt = createNewAttempt(exam, student, 1);
} else if (FINISHED_STATUSES.contains(latestAttemptOpt.get().getStatus())) {
if (!exam.isAllowRetry()) {
throw new IllegalStateException("Exam already completed. Retries are not permitted.");
}
attempt = createNewAttempt(exam, student, latestAttemptOpt.get().getAttemptNumber() + 1);
} else {
attempt = latestAttemptOpt.get();
}
3. The Rollback Bug: Graded, Then Erased
All timing rules run on the server. When a student saves an answer after the deadline, the service grades the attempt and rejects the request. My original code did this:
// Before PR #25
@Transactional
public void saveAnswer(Long attemptId, SubmitAnswerRequest req, UserPrincipal principal) {
// ...
long remaining = calculateRemainingSeconds(attempt.getExamination(), attempt, LocalDateTime.now());
if (remaining <= 0) {
autoFinalizeAttempt(attempt, attempt.getExamination()); // writes score + status
throw new IllegalStateException("Exam duration has expired. Attempt automatically submitted.");
}
// ...
}
Looks fine, right? But Spring rolls a @Transactional method back for any unchecked exception. IllegalStateException is unchecked, so the score, the status and the graded answers written by autoFinalizeAttempt were all rolled back. The attempt stayed in_progress, and the next request did the same thing again, forever. The message said "automatically submitted", but nothing was saved.
My fix keeps the rejection and commits the grade. A dedicated exception makes the intent explicit:
/**
* Thrown after an attempt whose time ran out has been finalized. Transactions declare
* noRollbackFor this exception so the finalized result is committed even though the
* client request (save answer, resume) is rejected. Mapped to 409 like other business-rule errors.
*/
public class AttemptExpiredException extends IllegalStateException { /* ... */ }
// noRollbackFor: when the time has run out, the attempt is finalized and that result must be
// committed even though the request is rejected with AttemptExpiredException.
@Transactional(noRollbackFor = AttemptExpiredException.class)
public void saveAnswer(Long attemptId, SubmitAnswerRequest req, UserPrincipal principal) {
// ...
if (remaining <= 0) {
autoFinalizeAttempt(attempt, attempt.getExamination());
throw new AttemptExpiredException("Exam duration has expired. Attempt automatically submitted.");
}
}
I gave startOrResumeAttempt the same annotation, because resuming after the deadline follows the same pattern. Since AttemptExpiredException extends IllegalStateException, the existing GlobalExceptionHandler still maps it to 409 Conflict, so I didn't have to touch the frontend at all.
What I took from this: if a method writes something on purpose and then throws, check whether the transaction will keep that write. By default, it won't.
To make sure it never comes back, I wrote a unit test that reproduces exactly this case:
@Test
void savingAnAnswerAfterTheDeadlineFinalizesTheAttempt() { /* status becomes completed, AttemptExpiredException thrown */ }
4. Students Who Leave: A Background Expiry Job
Before PR #25, an attempt was finalized only when the student made another request after the deadline. If they closed the browser, lost power or simply walked away, the attempt stayed in_progress. The teacher's results page showed the student as still working, and the grade book had a hole in it.
I wrote ExpiredAttemptFinalizer to close that gap:
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(name = "app.exam.finalizer.enabled", havingValue = "true", matchIfMissing = true)
public class ExpiredAttemptFinalizer {
private final ExamAttemptRepository attemptRepository;
private final ExamRunnerService examRunnerService;
@Scheduled(
initialDelayString = "${app.exam.finalizer.initial-delay-ms:30000}",
fixedDelayString = "${app.exam.finalizer.interval-ms:60000}")
public void finalizeExpiredAttempts() {
List<Long> expiredIds = attemptRepository.findExpiredInProgressAttemptIds(LocalDateTime.now());
for (Long attemptId : expiredIds) {
try {
examRunnerService.finalizeIfExpired(attemptId);
} catch (ObjectOptimisticLockingFailureException e) {
log.debug("Attempt {} was finalized concurrently by another request or instance", attemptId);
} catch (RuntimeException e) {
// One broken attempt must not stop the others from being finalized.
log.warn("Could not finalize expired attempt {}: {}", attemptId, e.getMessage());
}
}
}
}
The query uses the same deadline rule as the Java code: whichever comes first, start time + duration or the exam's end time.
@Query(value = """
SELECT a.id
FROM exam_attempts a
JOIN examinations e ON e.id = a.examination_id
WHERE a.status = 'in_progress'
AND LEAST(a.started_at + make_interval(mins => e.duration_minutes), e.end_at) <= :now
ORDER BY a.id
""", nativeQuery = true)
List<Long> findExpiredInProgressAttemptIds(@Param("now") LocalDateTime now);
There are three design choices here I think are worth explaining:
- The job only collects IDs. Grading happens in
examRunnerService.finalizeIfExpired(id), which is@Transactional. Because the call goes from one bean to another, it passes through the Spring proxy and each attempt gets its own transaction. If I had put the loop inside the same class (self-invocation), the annotation would be ignored and one failure could roll back everyone. finalizeIfExpiredre-checks the state inside its transaction (status == in_progressand remaining time ≤ 0). Between the query and the update, the student may already have submitted.- A partial index keeps the query cheap, because only a handful of rows are ever
in_progress:
CREATE INDEX idx_exam_attempts_in_progress
ON exam_attempts (examination_id)
WHERE status = 'in_progress';
I enable scheduling with a tiny @EnableScheduling config class, and you can tune or switch off the job with APP_EXAM_FINALIZER_ENABLED and APP_EXAM_FINALIZER_INTERVAL_MS.
5. Race 1: Double Click on "Start Exam"
Picture two POST /exams/{id}/start requests arriving within milliseconds (a double click, or the exam open in two tabs). Both read "no attempt yet" and both insert attempt number 1. I can't prevent this with an if in the application, because both requests pass the if before either one commits.
The database can, though. Migration V3__exam_attempt_integrity.sql adds a unique key, after first renumbering any duplicates that already exist so the migration can't fail on old data:
-- 1) Renumber attempts per (exam, student) in start order. No rows are deleted.
WITH ranked AS (
SELECT id,
ROW_NUMBER() OVER (PARTITION BY examination_id, student_id ORDER BY started_at, id) AS rn
FROM exam_attempts
)
UPDATE exam_attempts a
SET attempt_number = r.rn
FROM ranked r
WHERE a.id = r.id
AND a.attempt_number <> r.rn;
-- 2) One row per attempt number: two simultaneous "start" requests can no longer create two attempts.
ALTER TABLE exam_attempts
ADD CONSTRAINT uq_exam_attempt_number UNIQUE (examination_id, student_id, attempt_number);
The losing request now fails with DataIntegrityViolationException. Showing that error to a student would be confusing, since their exam did start. So I made the controller retry once:
@PostMapping("/{id}/start")
public ResponseEntity<ApiResponse<ExamStartResponse>> startExam(@PathVariable Long id,
@AuthenticationPrincipal UserPrincipal principal) {
ExamStartResponse response;
try {
response = examRunnerService.startOrResumeAttempt(id, principal);
} catch (DataIntegrityViolationException e) {
// Two "start" requests raced and the unique key rejected the second insert.
// The first attempt now exists, so retrying once simply resumes it.
response = examRunnerService.startOrResumeAttempt(id, principal);
}
return ResponseEntity.ok(ApiResponse.success(response, "Exam session started"));
}
I put the retry in the controller on purpose. The failed transaction is marked rollback-only, so retrying inside the service would reuse a broken transaction. Calling the service again from outside starts a fresh one, which sees the committed attempt and resumes it.
6. Race 2: Submit vs. the Expiry Job
The second race happens between finalization paths. The student clicks "Submit" at 10:59:59.8, and the expiry job (or another backend instance) picks up the same attempt at 11:00:00.1. Both load it as in_progress, both grade it, and both broadcast EXAM_SUBMITTED.
Here I went with optimistic locking instead of a distributed lock:
/**
* Optimistic lock: when a student's submit and the background finalizer (or two instances)
* race on the same attempt, only one update wins and the other fails instead of grading twice.
*/
@Version
@Column(nullable = false)
private Long version;
ALTER TABLE exam_attempts ADD COLUMN version BIGINT NOT NULL DEFAULT 0;
Hibernate now writes UPDATE exam_attempts SET ..., version = 4 WHERE id = ? AND version = 3. The second transaction updates zero rows and gets ObjectOptimisticLockingFailureException. The job just logs it and moves on; a user request gets a 409 with "reload and try again".
autoFinalizeAttempt also returns early when the attempt is no longer in_progress, which covers the common case before it even reaches a database conflict:
@Transactional
public ExamAttempt autoFinalizeAttempt(ExamAttempt attempt, Examination exam) {
if (attempt.getStatus() != AttemptStatus.in_progress) {
// Already finalized, e.g. by the background job or a parallel submit.
return attempt;
}
// ...grade
}
Why not a lock? A SELECT ... FOR UPDATE or a Redis lock would serialize every submit, add a timeout I'd have to tune, and leave a lock behind if an instance dies. Conflicts here are rare (a few per exam at most), so I decided detecting them is cheaper than preventing them.
Five violations (tab switches, leaving fullscreen) also call autoFinalizeAttempt, so that path is covered by the same guard.
7. Grading Rules You Can Unit Test
My old grading code lived inside autoFinalizeAttempt, mixed in with repositories and status changes. Short answers fell through both branches of an if, so they were never graded: always 0 points, and never offered to the teacher either. In PR #25 I moved the rules into a pure class with no Spring or JPA:
public final class AnswerGrader {
/** Separates alternative accepted answers in a short-answer key, e.g. "Jakarta|DKI Jakarta". */
public static final String ALTERNATIVE_SEPARATOR = "|";
public static boolean isAutoGraded(QuestionType type, String correctAnswer) {
return switch (type) {
case multiple_choice, true_false -> true;
case short_answer -> correctAnswer != null && !correctAnswer.isBlank();
case essay -> false;
};
}
public static boolean isCorrect(QuestionType type, String answer, String correctAnswer) {
if (answer == null || correctAnswer == null) return false;
return switch (type) {
case multiple_choice, true_false -> answer.trim().equalsIgnoreCase(correctAnswer.trim());
case short_answer -> {
String given = normalize(answer);
yield !given.isEmpty() && Arrays.stream(correctAnswer.split("\\|"))
.map(AnswerGrader::normalize)
.filter(accepted -> !accepted.isEmpty())
.anyMatch(given::equals);
}
case essay -> false;
};
}
/** Removes accents, lower-cases, collapses whitespace, drops punctuation at both ends. */
public static String normalize(String value) {
String text = Normalizer.normalize(value, Normalizer.Form.NFD).replaceAll("\\p{M}", "");
text = text.toLowerCase(Locale.ROOT).trim().replaceAll("\\s+", " ");
return text.replaceAll("^[\\p{Punct}\\s]+|[\\p{Punct}\\s]+$", "");
}
/** Score from 0 to 100, rounded down; 0 when the exam has no points. */
public static int scorePercent(int earnedPoints, int possiblePoints) {
return possiblePoints > 0 ? (earnedPoints * 100) / possiblePoints : 0;
}
}
Because it's pure, the tests read like a specification:
@Test
void shortAnswerAcceptsAnyAlternativeAfterNormalization() {
String key = "Jakarta|DKI Jakarta";
assertTrue(AnswerGrader.isCorrect(QuestionType.short_answer, " Jakarta. ", key));
assertTrue(AnswerGrader.isCorrect(QuestionType.short_answer, "dki JAKARTA", key));
assertTrue(AnswerGrader.isCorrect(QuestionType.short_answer, "Bogotá", "Bogota"));
assertFalse(AnswerGrader.isCorrect(QuestionType.short_answer, "Bandung", key));
}
I tightened the rest of the grading flow too:
- Blank answers score 0 and are not "pending". Previously a blank essay pushed the attempt into
needs_gradingand gave the teacher nothing to grade. - A short answer without a key goes to the teacher, just like an essay (
correct == nullmarks it as pending). gradeEssaykeeps the attempt inneeds_gradinguntil every pending answer has a grade. Before, grading the first of three essays marked the whole attemptcompletedwith a partial score.- Points are validated between 0 and the question's maximum, auto-graded questions can't be overwritten by hand, and an attempt that is still in progress can't be graded.
@Test
void manualGradingKeepsAttemptPendingUntilEveryEssayIsGraded() {
// two essays worth 4 and 6 points
service.gradeEssay(ATTEMPT_ID, 11L, EssayGradingDto.builder().pointsEarned(4).build(), teacherPrincipal);
assertEquals(AttemptStatus.needs_grading, attempt.getStatus());
service.gradeEssay(ATTEMPT_ID, 12L, EssayGradingDto.builder().pointsEarned(3).feedback("Add an example").build(), teacherPrincipal);
assertEquals(AttemptStatus.completed, attempt.getStatus());
assertEquals(70, attempt.getScore()); // 7 of 10 points
}
8. Live Monitor Events Only After Commit
There's one more integrity detail: the teacher's live monitor. I used to send events like ANSWER_SAVED and EXAM_SUBMITTED with SimpMessagingTemplate in the middle of the transaction. If the transaction then rolled back (which, as section 3 showed, really happened), the teacher had already seen a score that didn't exist.
Now ExamRealtimeService registers the event and only sends it after the commit:
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
dispatch(envelope);
}
});
} else {
dispatch(envelope);
}
The dispatch method is also where Redis fan-out for several instances plugs in. I cover that in Part 7.
9. Checklist
| Rule | How it's enforced |
|---|---|
| Retakes never crash | findFirst...OrderByAttemptNumberDesc |
| Finished attempts can't be resumed | FINISHED_STATUSES covers 4 states |
| A late request still saves the grade | AttemptExpiredException + noRollbackFor |
| Abandoned attempts get a result | ExpiredAttemptFinalizer every 60 s, one transaction per attempt |
| No duplicate attempt numbers | UNIQUE (examination_id, student_id, attempt_number) + one retry |
| No double grading | @Version + early return in autoFinalizeAttempt |
| Short answers graded fairly | AnswerGrader.normalize and ` |
| Manual grading is complete before "completed" | Pending check in gradeEssay |
| Monitor never shows rolled-back data | afterCommit dispatch |
Conclusion & What's Next
Looking back, none of these bugs showed up in a quick manual test with one user and a fast connection. They only appeared with retakes, double clicks, closed tabs and two code paths finishing the same attempt. What fixed them in the end were database guarantees (a unique key, a version column, transaction boundaries), not more if statements.
The @Version column and the per-attempt transactions also prepared the ground for my next step: running more than one backend instance. In Part 7, I move the refresh token into an httpOnly cookie, give the SPA a Content-Security-Policy, and teach rate limits, live events and file uploads to work across several instances.
- Live demo: https://spring-elearning.mfarim.com
- Source code: github.com/mfarim/spring-elearning-react
- Pull request: #25

Written by Muhammad Fari Madyan
Senior Full Stack Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

