box-node-sdk icon indicating copy to clipboard operation
box-node-sdk copied to clipboard

Box allows folder creation with duplicate name

Open chaoyangnz opened this issue 3 years ago • 4 comments

  • [x] I have checked that the SDK documentation doesn't solve my issue.
  • [x] I have checked that the API documentation doesn't solve my issue.
  • [x] I have searched the Box Developer Forums and my issue isn't already reported (or if it has been reported, I have attached a link to it, for reference).
  • [x] I have searched Issues in this repo and my issue isn't already reported.

Description of the Issue

When I use SDK to create folder with multiple requests, each one would create the folder and check status code. I expect 409 to tell me if there are conflicts.

But 409 is not always triggered. Please tell me how I can figure out my scenarios.

Steps to Reproduce

const folders = await Promise.all([
client.folders.create('0', 'aa'),
client.folders.create('0', 'aa'),
client.folders.create('0', 'aa'),
client.folders.create('0', 'aa'),
client.folders.create('0', 'aa')
])

expect(new Set(folders.map(folder => folder.id)).size).toBe(1);

Expected Behavior

Always tell the client caller with 409 and disallow the folder name with the same name.

Error Message, Including Stack Trace

I got several 409, but two 201. then two folders with the same name got created.

Screenshots

Versions Used

Node SDK: latest

chaoyangnz avatar Mar 22 '21 08:03 chaoyangnz

Hmmm that is odd, since the 409's are being returned from the API this might be more symptomatic of the underlying API than that of the SDK as I believe the Node SDK just wraps the API and hands back to the client whatever the API response is without any middle processing. I believe I have seen this when there is the same call in quick succession to the API although it's not ideal that the 409's are not consistently catching. The only way I can think to remediate this in the short term is to make a call first the to retrieve all folders in the current directory you are creating the new folders in via this api. You can pass in the filter field: type as folder and create a check to make sure the current folder name you are attempting to create does not already exist in the list that is returned. Although you will need to traverse this collection so this may not be ideal if the projected size of the directory you are creating these folders in gets too large. Apologies if this is not entirely helpful but wanted to throw out some ideas to help unblock in the immediate time frame, any thoughts @sujaygarlanka?

carycheng avatar Mar 23 '21 06:03 carycheng

@carycheng thanks for you reply. We did exactly what you are describing that traversing the parent folder and check existence. but it is still happening to create duplicate folders.

I think there is a race condition on creating and there is no control in Box server side when two creating requests are processing but none of them complete.

We integrates a couple of Cloud services, typically like Dropbox, Google Drive with very similar logic. Only Box has the odd behavior. And we do the logic check-existence-and-create in multiple nodes / servers (not a single process)

We are designing a workaround to mitigate the issue by enforcing a distributed lock, but still it should be ideal if Box can control the race condition once a request is received and in-processing and another request for same folder creation is coming.

chaoyangnz avatar Mar 23 '21 06:03 chaoyangnz

Hi @chaoyangnz ,

Thanks for submitting this Issue! As @carycheng mentioned, this is almost certainly an issue with the underlying API, and not the SDK, itself. I will reach out to the internal service owners of the create folder endpoint and see if they are aware of this. I will get back to you ASAP!

@PJSimon

PJSimon avatar Mar 26 '21 17:03 PJSimon

Seconding that this is not an SDK specific issue, but an underlying API issue when calling the folder create endpoint very quickly. Typically it's recommended you try and space out folder create operations slightly if you can or track the creation after the fact to ensure 1:1 folder creation. This can be done with events or a scan at the end of a bulk creation transaction.

eumenhofer avatar Jun 09 '21 23:06 eumenhofer

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

stale[bot] avatar Dec 19 '22 19:12 stale[bot]

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box Node SDK and feel free to open another PR/issue at any time.

stale[bot] avatar Dec 26 '22 20:12 stale[bot]