Bug bounty — “unsolvable problem”

bug bounty
  • Before4 programmers
  • Analysis~15 h
  • Rewrite~20 h
  • After50+ scanners

Four programmers had already tried.Ten barcode scanners and the server stopped answering. I followed the logs, bet on SQLite locks, and moved the database to Postgres. Then it held.

"Hey, we've got a problem in production. Four programmers have tried and can't crack it, we've already burned xxx CZK on it. Want to take a look?"

Sure! I love a challenge. This software was for big companies, Kaufland and the like. It was basically a server, an Electron dashboard, and EAN barcode scanners. The software is used to stocktake warehouse inventory in large operations.

The problem was that once 10+ scanners connected, the backend stopped responding and got swamped. People roughly knew where in the code the problem sat, but not why. I did the whole thing remote. I had a great person who was my hands and feet — every fix, he had to pull it, install it, connect the scanners by hand one after another, and let it run. That must have been miserable 😅.

Time was pressing and it was pretty stressful. I went for an exact process instead of guessing or magic 😁. I had the tester run it about three times, and after each run I collected more and more logs. I filtered the logs, and this is where ChatGPT gave me a hand (I think this was the GPT-4 era), which had just started being able to do those Python analyses and generate charts. Through the charts I could see the anomalies, and then I knew roughly where to look. One function was the problem. It iteratively updated items in the db and recalculated them. On mock data, a single call did hundreds of read-writes to the db. And when that function took ~1s to finish, it meant that 10 or more connected scanners, running the same load tests every 10 seconds, were already swamping the server. So the throughput was obvious, and it made sense that latency climbed at the 10th scanner, and with each extra one the server got swamped and stopped responding. After the scanners were switched off and fewer than 10 stayed connected, the server recovered in a moment, cleared everything, and it was fine again.

I remember forming a theory at the time: the problem had to be the locks in the SQLite db that was in use. It isn't built for concurrent writes to the same tables and rows — SQLite just locks, and the other requests have to wait for the lock to drop before they can write.

But a theory is one thing and practice is another. To verify it I had to move the db to Postgres, which handles concurrent writes really well. That meant a pretty brutal refactor of the whole app. I spent about 15 hours on the analysis and getting to the theory, then about another 20 hours rewriting parts of the system, without knowing whether it would work.

I didn't have a big budget for it, so for me it had to be a one-shot success, or it wouldn't even pay to take it on 😆. The fix did work in a local simulation. I remember it like it was yesterday: I sent the build to the tester, lay on the floor, prayed, and waited to see if it would hold up in production too. The message that 50+ scanners were connected and the system was running healthy was an unbelievable relief, and a real satisfaction 🙂. Based on the calculations, the system should be able to handle up to ~230 scanners at maximum load.

What I took from it

It confirmed for me that the approaches I use are good ones. Especially the approach of following the facts, and not burning time where there is no signal. And also how important it is to know the limits of the technologies you start using, because the wrong choice can get very expensive — not only in money, but in reputation, frustration, relationships, and the rest.