C interop spookiness with Swift 5

We're in the process of adding Swift 5 support for our libbson binding in the MongoDB Swift driver, and I've run into a strange issue. Since bson_t is now imported as an opaque type, I had to add an accessor for that value in our bindings to libbson. What's happening now is that when I first compile and run our tests, that value will return a value of 1 until I modify the calling function in some way (add a print statement, move a line). Once I modify that function, it will correctly return the length of the bson_t and everything works as expected.

I've been able to confirm that the helper function is correctly reporting the value of the passed in bson_t, so it seems like the bson_t being passed in is invalid for some reason. Any ideas what might cause this behavior?

I have seen similar issues in my own code using C interop and have found that it is usualy caused by an underlying race condition that the print() statement simply avoids by adding a tiny, but consistent delay.

This may or may not be the case for you (I'm not familiar with libbson), but it was for me when working with the inotify library.

I second the threading concern. Maybe try running ASAN/TSAN to see if you can narrow down an issue.