← All writing

Two clicks, one ticket

A member double-clicks "Open a ticket" and gets two tickets, past the limit of one. The check was there. It just ran twice before either click had finished. How I made a button do its work once.

·2 min read

My Discord bot has support tickets. A server can say “one open ticket per person”. The code checks it:

  1. count this person’s open tickets
  2. if they’re at the limit, say so and stop
  3. otherwise make the channel and save the ticket

That’s correct for one click. For two clicks a tenth of a second apart, it’s wrong. Both count zero. Both pass. Both make a channel. The member now has two tickets and the limit is one.

The same thing happened to the staff buttons. Claim twice posted “Claimed by” twice. Close twice posted “Ticket closed” twice and sent the transcript twice. Reopen twice posted “reopened” twice.

Each press runs on its own. There’s no shared memory between them, so “am I already doing this?” can’t be a variable.

Let the database decide

The only thing both presses share is the database. So the answer has to be a single statement that only one of them can win.

For closing, it’s small. The old statement was “set this ticket to closed”. The new one is “set this ticket to closed if it isn’t closed already”, and then look at how many rows changed.

UPDATE tickets SET state = 'closed', closed_at = ?2
WHERE thread_id = ?1 AND state != 'closed'

The first press changes one row and carries on: the post, the log line, the transcript. The second changes zero rows and stops. No lock, no timer. The database already knows how to do one thing at a time.

Claim and reopen work the same way: the write carries its own condition, and “zero rows changed” means someone got there first.

Opening is harder

Opening has no row yet to put a condition on. The limit is a count, and two presses can both count before either has written.

So opening takes a turn first. One small row per person, written with “insert, or do nothing if it’s there”. Whoever inserts it goes ahead, counts again now that it’s their turn, and opens the ticket. The other press is told “your ticket is being opened, one moment”.

The turn is given back when the ticket is open. And it expires by itself after 30 seconds, because code that takes a turn and then crashes must never lock a person out for good.

Undoing a half-done reopen

One more detail. Reopen now flips the ticket to “open” first, then tells Discord. If Discord refuses, for example because the channel was deleted by hand, the ticket would be left “open” with no channel.

So when Discord says no, the row is put back exactly as it was: closed, with its old closing time. A button that fails should leave things the way it found them.

The test

The test for all of this is short: press the same button twice at the same moment and count. One ticket. One “closed” post. One “reopened” post.

I ran those tests against the old code first. Four of five failed, which is how I knew they were testing the right thing.