Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Await updateD1Data to avoid race conditions #217

Closed
patdx opened this issue Dec 14, 2023 · 2 comments · Fixed by #227
Closed

Await updateD1Data to avoid race conditions #217

patdx opened this issue Dec 14, 2023 · 2 comments · Fixed by #227

Comments

@patdx
Copy link
Contributor

patdx commented Dec 14, 2023

I've been experimenting with a fork of this code, and noticed that sometimes my edits did not actually save in the production CF Workers environment. I could not reproduce it locally. It also did not seem to be cache related, even if I cleared the cache it still happened. I suspect it's related to this code:

const result = updateD1Data(d1, data.table, data);

const result = updateD1Data(d1, data.table, data);

The function updateD1Data is async and returns a Promise, but the code seems to be written as if it is a synchronous function. Maybe if the HTTP request responds very quickly, the CF Workers runtime tries to suspend the worker isolate before the floating updateD1Data promise resolves, so the update does not actually get committed to the D1 Database.

Even if that is not actually the case, I think this is undefined behavior to have a floating promise to update the database. It may be a good idea to do either:

  1. await the updateD1Data call in every instance that it is used, or
  2. Call cloudflare's context.waitUntil eg: context.waitUntil(updateD1Data(d1, data.table, data))

I did strategy 1 in my fork and it seemed to resolve the issue. But it's hard to say 100% as it only happened in CF Workers, not locally.

@lane711
Copy link
Owner

lane711 commented Dec 14, 2023 via email

@patdx
Copy link
Contributor Author

patdx commented Jan 27, 2024

Hi @lane711 sorry for the long delay. Between end of year holidays and other responsibilities I didn't have time to look at this for a while. Finally I found some time to catch up on my GitHub backlog this week. I submitted a PR for the issue above. It's a very simple PR. Let me know if you need anything else and I'll try to adjust it. If you would like to resolve the issue faster please feel free to make any necessary changes yourself, I won't mind. Thanks! #227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants