← All posts

Engineering · 2026.10.10 · 5 min

Code Review After the Rust Port: 15 Findings, 6 Carried Over from Java

Four lessons from a full code review after a Java-to-Rust port: transaction semantics copied from a misleading annotation, sqlx returning Ok on failed commits, disconnects killing handlers, and suspension sharing the lending flag.

Reading
Theme
Size
Width

The previous post covered replacing the backend wholesale, from Spring Boot to Rust. After merging to main, I ran Claude Code's /code-review at its highest level over the entire port PR and got 15 findings. Six of them were bugs from the Java days that the port carried over as-is. That was expected: the porting principle was to preserve old behavior, and parity checks can only prove the two sides match. They can't catch places where both are wrong in the same way.

The issues that could actually hurt someone included service outages, operations failing silently, accounting holes, and broken alerts, plus unauthenticated endpoints missing input validation and rate limits. All of them are fixed. Below are the four most worth taking away.

Ported from the annotation, ported wrong

Simplified, the Java audit service looked like this:

public void record(/* ... */) {
    recordInTransaction(/* ... */); // self-invocation inside the same object, does not go through the Spring proxy
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void recordInTransaction(/* ... */) { /* write the audit record */ }

Reading the annotation, you'd think audit opens a new transaction and writes independently, and the Rust version did exactly that with a separate connection. But record() calls it from inside the same object without going through Spring's proxy, so REQUIRES_NEW never took effect. What Java actually did was run audit in the caller's transaction: if the audit write failed, the whole action rolled back.

Following the annotation changed the behavior. The fix was to go back to the caller's transaction. For events that must be recorded even when the action is rejected, like a failed login, the transaction is ended first and the audit is written after. Pulling on this thread turned up several more paths that held a transaction while grabbing a second connection from the pool, and those were changed to use a single connection inside the transaction.

Before and after: the REQUIRES_NEW annotation suggests audit opens a new transaction on a separate connection, and the Rust port did exactly that; but Java's record() called it inside the same object, bypassing Spring's proxy, so REQUIRES_NEW never took effect, audit ran in the caller's transaction, and a failed audit write rolled back the whole action. The fix returns audit to the caller's transaction and writes failed-login events after ending the transaction.

When porting, go by actual behavior and treat annotations and comments as a reference only. For me that was the biggest lesson of the whole port.

sqlx returns Ok when committing a transaction that already failed

In a Postgres transaction, once any statement fails, the transaction enters the aborted state, and a later COMMIT is treated as a rollback. Postgres doesn't raise an error. It just returns a ROLLBACK tag, and sqlx returns Ok when it gets one.

The pause-lending loop had a section that swallowed SQL errors and kept going. The final commit returned Ok, the caller assumed success, and in fact none of the data had been written.

The fix was to read the ReadyForQuery status in a vendored copy of sqlx-postgres and return an error when committing an aborted transaction. The pause loop now wraps each lending config in a savepoint, so one failure doesn't drag the others down with it.

Sequence diagram: the pause-lending loop sends SQL, one statement fails and the error is swallowed, so the Postgres transaction is aborted; the later COMMIT is treated as a rollback, Postgres returns a ROLLBACK tag without an error, sqlx returns Ok, and the caller assumes success though nothing was written. The fix reads the ReadyForQuery status so commit errors, and wraps each lending config in a savepoint.

When the client disconnects, the handler is gone

In the Servlet world, a request that comes in runs to completion. If the client disconnects partway, the worst case is that the response can't be written. hyper 1.x is different: when the client disconnects, it drops the handler's future outright, so any await can be the end of the line.

That means if a user clicks pause lending and closes the tab before the transaction commits, the transaction rolls back. If an admin manually triggers the engine and the request gets cut off, the lock it holds is never released.

The fix was to spawn every request into its own tokio task at the outermost layer of the router, so a client disconnect only throws away the response, the same as with servlets. The circuit breaker's probe permits and the distributed lock were also changed to be returned on drop.

Illustration of a client disconnecting mid-request: with servlets the request runs to completion and only the response can't be written; hyper 1.x drops the handler's future, so it never reaches commit, the transaction rolls back, and a held lock is not released; after the fix each request runs in its own tokio task and only the response is lost.

Keep user intent and billing eligibility separate

The Java version suspended a user by switching off their lending toggle. The problem is that users press pause and Resume themselves too. One flag meant both "does the user want to lend" and "is this account allowed to lend," so a suspended user could get around the suspension just by pressing Resume.

Now suspension leaves the flag alone. The engine excludes suspended users when it picks lending configs, pressing Resume while suspended returns 400, and once payment resumes, any pause the user set themselves is kept. I also considered keeping the flag flip and storing the pre-suspension value separately, but the two intents would still share one flag, and the bypass would just come back in a different form.

Looking back

The 15 findings were split into 7 groups, each fixed in parallel in its own worktree with its own test DB. Every finding needed a regression test that fails before the fix and passes after. At merge time each one was reviewed individually, and after every merge the next PR pulled in main and reran CI, because two PRs with no textual conflict can still fail to compile together.

If the porting principle is to preserve old behavior, green parity only means the two sides match. After the port, you need another review over the entire diff to find the bugs that came along for the ride.

#Database Transactions#PostgreSQL#Rust#Code Review

Letters

Loading comments…

Next

Investing

Naked Candlestick Trading as Backtestable Rules: Out of Sample, It Lost to Random Entries

I turned a YouTuber's naked candlestick method into rules, backtested it on daily bars for over 1,000 Taiwan stocks against a random-timing control, and out of sample it lost by 2.62 standard deviations.