Skip to content

Address gethostname failure due to missing WinSock initialization #1363

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

Merged
merged 1 commit into from
Mar 31, 2025

Conversation

eramongodb
Copy link
Contributor

@eramongodb eramongodb commented Mar 31, 2025

Addresses the following stderr output by the test_bson test binary on Windows:

unable to get hostname: 0

This message is emitted by a call to gethostname in _bson_context_get_hostname by the following stack trace:

bsoncxx::v_noabi::oid::oid()
  - bson_oid_init(bson_oid_t *oid, bson_context_t *context)
  - bson_context_get_default()
  - _bson_context_init_default()
  - _bson_context_init(bson_context_t *context, bson_context_flags_t flags)
  - _bson_context_init_random (bson_context_t *context, bool init_seq)
  - _bson_context_get_hostname (char out[HOST_NAME_MAX])
  - gethostname (char *name, int namelen)

Patching the mongoc library to print the value of WSAGetLastError (rather than errno) reveals an error code value of WSANOTINITIALIZED. This reveals that gethostname, a function provided by the WinSock library, is being invoked before a successful call to WSAStartup. This call is usually handled by mongoc_init. However, test_bson does not use the mongoc or mongocxx library. Therefore, it does not call mongoc_init or WSAStartup before calling bson_oid_init.

This PR proposes a straightforward workaround to manually invoke WSAStartup prior to executing the test suite on Windows. Filed CDRIVER-5957 to investigate a more thorough solution to this problem (e.g. document that mongoc_init or WSAStartup must have been invoked first? add internal calls to WSAStartup/WSACleanup that wrap the gethostname on Windows?).

@eramongodb eramongodb requested a review from kevinAlbs March 31, 2025 14:35
@eramongodb eramongodb self-assigned this Mar 31, 2025
Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice investigation.

Filed CDRIVER-5957

Commented with idea for alternative.

@eramongodb eramongodb merged commit 5859e06 into mongodb:master Mar 31, 2025
12 of 21 checks passed
@eramongodb eramongodb deleted the cxx-test_bson branch March 31, 2025 19:29
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 this pull request may close these issues.

2 participants