Skip to content

Rare chance of 404 when making more than one request per second #16

@wyj8bc5odzfl

Description

@wyj8bc5odzfl

This is a follow up from the rejected pull request #15

To give a bit more context on how I encountered the issue (and why it is reasonable to expect it to occur again for someone else), here is what I was doing:
I have a script which calls the UsersByRestIds endpoint for a large amount of users.
I had one call that started at second 12.24, and then the next call started at second 12.85 (0.6 second gap).
Since both calls took place at the same second, the x-client-transaction-id contained the same timestamp.

The 12.24 call worked just fine, the 12.85 call got a 404.

Further examination led me to notice that both calls used the exact same x-client-transaction-id header.
When you inspect the code of generate_transaction_id(), you will notice that the only thing that could cause them to differ in this circumstance is the value of random_num.

Indeed, the second request was unlucky enough the use the same random_num value as the first.

Testing this further, I tried setting random_num to a fixed value, so that I could verify that Twitter rejects reuse of the header. I saw the pattern of first request works, second or third fails.
(As a note: I think that I was always experiencing a 404 on the second request originally, but when testing right now, I'm getting it on the third. I wonder if this is to make it a less likely problem for real Twitter usage, since they don't account for this in the client.)

In my pull request, I stated that:

Twitter returns a 404 if the value used for the XOR in generate_transaction_id() is used more than once in a given second.

and suggested ensuring that unique pairs of (time_now, random_num) are used when generating the x-client-transaction-id header. I still think that something along those lines would work, but there are some other aspects that could be taken into consideration:

  • I have not tested if you can reuse a (time_now, random_num) value across different endpoints within a second, which, if you can, implies that it is only the full x-client-transaction-id that cannot be reused
  • While you may not be able to reuse a header immediately, you may be able to use it again after some short time has passed. I think I saw this occurring when I was first trying to understand the header, but I haven't been back to study it more.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions