While it's technically possible the race condition occurred in this case (i.e., the link was created by some other client in between your two calls to list_shared_links and create_shared_link_with_settings), that is relatively unlikely given the short time span you indicated.
Is it possible your app called create_shared_link_with_settings twice somehow though? E.g., as an automatic retry in response to a network failure? That could cause this.
In any case though, you should implement error handling in your app for the shared_link_already_exists error, regardless of the timing of a previous call to list_shared_links.
Even though you call list_shared_links and create_shared_link_with_settings in the same function, there's still a race condition between the two calls, so you should make sure your call to create_shared_link_with_settings can always handle the possibility of a shared_link_already_exists error.
No, having a parent folder shared wouldn't cause a shared_link_already_exists error for a file in that folder.