Skip to content

Commit bdb1421

Browse files
authored
Removing assert on null singleton as cases are handled with proper error handling (microsoft#477)
* Removing assert on null singleton as cases are handled with proper error handling * Fixing test
1 parent 0247231 commit bdb1421

16 files changed

+82
-72
lines changed

Source/Common/Android/utils_android.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ NAMESPACE_XBOX_HTTP_CLIENT_BEGIN
99

1010
JNIEnv* get_jvm_env()
1111
{
12-
auto httpSingleton = xbox::httpclient::get_http_singleton(true);
13-
HC_PERFORM_ENV* platformContext = httpSingleton->m_performEnv.get();
14-
JavaVM* javaVm = platformContext->GetJavaVm();
12+
JNIEnv* jniEnv = nullptr;
13+
jint jniResult = JNI_ERR;
1514

16-
if (javaVm == nullptr)
15+
auto httpSingleton = xbox::httpclient::get_http_singleton();
16+
if (httpSingleton)
1717
{
18-
HC_TRACE_ERROR(HTTPCLIENT, "javaVm is null");
19-
throw std::runtime_error("JavaVm is null");
20-
}
18+
HC_PERFORM_ENV* platformContext = httpSingleton->m_performEnv.get();
19+
JavaVM* javaVm = platformContext->GetJavaVm();
2120

22-
JNIEnv* jniEnv = nullptr;
23-
jint jniResult = javaVm->GetEnv(reinterpret_cast<void**>(&jniEnv), JNI_VERSION_1_6);
21+
if (javaVm == nullptr)
22+
{
23+
HC_TRACE_ERROR(HTTPCLIENT, "javaVm is null");
24+
throw std::runtime_error("JavaVm is null");
25+
}
26+
27+
jniResult = javaVm->GetEnv(reinterpret_cast<void**>(&jniEnv), JNI_VERSION_1_6);
28+
}
2429

2530
if (jniResult != JNI_OK)
2631
{

Source/Global/global.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,9 @@ http_singleton::~http_singleton()
4141
m_mocks.clear();
4242
}
4343

44-
std::shared_ptr<http_singleton> get_http_singleton(bool assertIfNull)
44+
std::shared_ptr<http_singleton> get_http_singleton()
4545
{
4646
auto httpSingleton = std::atomic_load(&g_httpSingleton_atomicReadsOnly);
47-
if (assertIfNull && httpSingleton == nullptr)
48-
{
49-
HC_TRACE_ERROR(HTTPCLIENT, "Call HCInitialize() fist");
50-
ASSERT(httpSingleton != nullptr);
51-
}
52-
5347
return httpSingleton;
5448
}
5549

Source/Global/global.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ typedef struct http_singleton
9090
} http_singleton;
9191

9292

93-
std::shared_ptr<http_singleton> get_http_singleton(bool assertIfNull);
93+
std::shared_ptr<http_singleton> get_http_singleton();
9494
HRESULT init_http_singleton(HCInitArgs* args);
9595
void cleanup_http_singleton();
9696

@@ -101,7 +101,7 @@ class shared_ptr_cache
101101
template<typename T>
102102
static void* store(std::shared_ptr<T> contextSharedPtr)
103103
{
104-
auto httpSingleton = get_http_singleton(false);
104+
auto httpSingleton = get_http_singleton();
105105
if (nullptr == httpSingleton)
106106
return nullptr;
107107
std::lock_guard<std::recursive_mutex> lock(httpSingleton->m_sharedPtrsLock);
@@ -115,7 +115,7 @@ class shared_ptr_cache
115115
template<typename T>
116116
static std::shared_ptr<T> fetch(void *rawContextPtr)
117117
{
118-
auto httpSingleton = get_http_singleton(false);
118+
auto httpSingleton = get_http_singleton();
119119
if (nullptr == httpSingleton)
120120
return nullptr;
121121

@@ -133,7 +133,7 @@ class shared_ptr_cache
133133

134134
static void remove(void *rawContextPtr)
135135
{
136-
auto httpSingleton = get_http_singleton(false);
136+
auto httpSingleton = get_http_singleton();
137137
if (nullptr == httpSingleton)
138138
return;
139139

Source/Global/global_publics.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ STDAPI
4444
HCSetGlobalProxy(_In_ const char* proxyUri) noexcept
4545
try
4646
{
47-
auto httpSingleton = get_http_singleton(false);
47+
auto httpSingleton = get_http_singleton();
4848
if (nullptr == httpSingleton)
4949
{
5050
return E_HC_NOT_INITIALISED;
@@ -65,7 +65,7 @@ HCSetHttpCallPerformFunction(
6565
return E_INVALIDARG;
6666
}
6767

68-
auto httpSingleton = get_http_singleton(false);
68+
auto httpSingleton = get_http_singleton();
6969
if (httpSingleton)
7070
{
7171
return E_HC_ALREADY_INITIALISED;
@@ -106,7 +106,7 @@ STDAPI_(int32_t) HCAddCallRoutedHandler(
106106
return -1;
107107
}
108108

109-
auto httpSingleton = get_http_singleton(true);
109+
auto httpSingleton = get_http_singleton();
110110
if (nullptr == httpSingleton)
111111
return E_HC_NOT_INITIALISED;
112112

@@ -120,7 +120,7 @@ STDAPI_(void) HCRemoveCallRoutedHandler(
120120
_In_ int32_t handlerContext
121121
) noexcept
122122
{
123-
auto httpSingleton = get_http_singleton(true);
123+
auto httpSingleton = get_http_singleton();
124124
if (nullptr != httpSingleton)
125125
{
126126
std::lock_guard<std::recursive_mutex> lock(httpSingleton->m_callRoutedHandlersLock);

Source/Global/mem.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ HCMemSetFunctions(
3333
_In_opt_ HCMemFreeFunction memFreeFunc
3434
) noexcept
3535
{
36-
if (xbox::httpclient::get_http_singleton(false) != nullptr)
36+
if (xbox::httpclient::get_http_singleton() != nullptr)
3737
{
3838
return E_HC_ALREADY_INITIALISED;
3939
}

Source/HTTP/Android/http_android.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ void Internal_HCHttpCallPerformAsync(
5353
_In_ HCPerformEnv env
5454
) noexcept
5555
{
56-
auto httpSingleton = xbox::httpclient::get_http_singleton(true);
56+
auto httpSingleton = xbox::httpclient::get_http_singleton();
57+
if (httpSingleton == nullptr)
58+
{
59+
HCHttpCallResponseSetNetworkErrorCode(call, E_HC_NOT_INITIALISED, 0);
60+
XAsyncComplete(asyncBlock, E_HC_NOT_INITIALISED, 0);
61+
return;
62+
}
63+
5764
std::unique_ptr<HttpRequest> httpRequest{
5865
new HttpRequest(
5966
asyncBlock,

Source/HTTP/WinHttp/winhttp_http_task.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ static void NetworkConnectivityHintChangedCallback(
10531053
{
10541054
UNREFERENCED_PARAMETER(context);
10551055

1056-
auto singleton = get_http_singleton(false);
1056+
auto singleton = get_http_singleton();
10571057

10581058
if (singleton != nullptr)
10591059
{
@@ -1076,7 +1076,7 @@ HRESULT Internal_InitializeHttpPlatform(HCInitArgs* args, PerformEnv& performEnv
10761076
void Internal_CleanupHttpPlatform(HC_PERFORM_ENV* performEnv) noexcept
10771077
{
10781078
#if HC_PLATFORM == HC_PLATFORM_GSDK
1079-
auto singleton = get_http_singleton(false);
1079+
auto singleton = get_http_singleton();
10801080
if (singleton != nullptr && singleton->m_networkModule != nullptr)
10811081
{
10821082
FreeLibrary(singleton->m_networkModule);
@@ -1159,7 +1159,7 @@ void CALLBACK Internal_HCHttpCallPerformAsync(
11591159
#if HC_PLATFORM == HC_PLATFORM_GSDK
11601160
if (XGameRuntimeIsFeatureAvailable(XGameRuntimeFeature::XNetworking))
11611161
{
1162-
auto singleton = get_http_singleton(true);
1162+
auto singleton = get_http_singleton();
11631163
if (singleton != nullptr)
11641164
{
11651165
if (singleton->m_networkModule == nullptr)

Source/HTTP/httpcall.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ try
3333
return E_INVALIDARG;
3434
}
3535

36-
auto httpSingleton = get_http_singleton(true);
36+
auto httpSingleton = get_http_singleton();
3737
if (nullptr == httpSingleton)
3838
return E_HC_NOT_INITIALISED;
3939

@@ -105,7 +105,7 @@ HRESULT perform_http_call(
105105
HRESULT hr = XAsyncBegin(asyncBlock, call, reinterpret_cast<void*>(perform_http_call), __FUNCTION__,
106106
[](XAsyncOp opCode, const XAsyncProviderData* data)
107107
{
108-
auto httpSingleton = get_http_singleton(false);
108+
auto httpSingleton = get_http_singleton();
109109
if (nullptr == httpSingleton)
110110
{
111111
return E_HC_NOT_INITIALISED;
@@ -259,7 +259,7 @@ bool http_call_should_retry(
259259
{
260260
auto retryAfterTime = retryAfter + responseReceivedTime;
261261
http_retry_after_api_state state(retryAfterTime, httpStatus);
262-
auto httpSingleton = get_http_singleton(false);
262+
auto httpSingleton = get_http_singleton();
263263
if (httpSingleton)
264264
{
265265
httpSingleton->set_retry_state(call->retryAfterCacheId, state);
@@ -371,7 +371,7 @@ void retry_http_call_until_done(
371371
_In_ HC_UNIQUE_PTR<retry_context> retryContext
372372
)
373373
{
374-
auto httpSingleton = get_http_singleton(false);
374+
auto httpSingleton = get_http_singleton();
375375
if (nullptr == httpSingleton)
376376
{
377377
HC_TRACE_WARNING(HTTPCLIENT, "Http call after HCCleanup was called. Aborting call.");
@@ -426,7 +426,7 @@ void retry_http_call_until_done(
426426
HC_UNIQUE_PTR<XAsyncBlock> nestedAsyncPtr{ nestedAsyncBlock };
427427
HC_UNIQUE_PTR<retry_context> retryContext{ static_cast<retry_context*>(nestedAsyncBlock->context) };
428428

429-
auto httpSingleton = get_http_singleton(false);
429+
auto httpSingleton = get_http_singleton();
430430
if (httpSingleton == nullptr)
431431
{
432432
HC_TRACE_WARNING(HTTPCLIENT, "Http completed after HCCleanup was called. Aborting call.");
@@ -507,7 +507,7 @@ try
507507
HRESULT hr = XAsyncBegin(asyncBlock, retryContext.get(), reinterpret_cast<void*>(HCHttpCallPerformAsync), __FUNCTION__,
508508
[](_In_ XAsyncOp op, _In_ const XAsyncProviderData* data)
509509
{
510-
auto httpSingleton = get_http_singleton(false);
510+
auto httpSingleton = get_http_singleton();
511511
if (nullptr == httpSingleton)
512512
{
513513
return E_HC_NOT_INITIALISED;

Source/HTTP/httpcall_request.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ try
2020
}
2121
RETURN_IF_PERFORM_CALLED(call);
2222

23-
auto httpSingleton = get_http_singleton(true);
23+
auto httpSingleton = get_http_singleton();
2424
if (nullptr == httpSingleton)
2525
return E_HC_NOT_INITIALISED;
2626

@@ -46,7 +46,7 @@ try
4646
return E_INVALIDARG;
4747
}
4848

49-
auto httpSingleton = get_http_singleton(true);
49+
auto httpSingleton = get_http_singleton();
5050
if (nullptr == httpSingleton)
5151
return E_HC_NOT_INITIALISED;
5252

@@ -70,7 +70,7 @@ try
7070
}
7171
RETURN_IF_PERFORM_CALLED(call);
7272

73-
auto httpSingleton = get_http_singleton(true);
73+
auto httpSingleton = get_http_singleton();
7474
if (nullptr == httpSingleton)
7575
return E_HC_NOT_INITIALISED;
7676

@@ -278,7 +278,7 @@ try
278278
{
279279
if (call == nullptr)
280280
{
281-
auto httpSingleton = get_http_singleton(true);
281+
auto httpSingleton = get_http_singleton();
282282
if (nullptr == httpSingleton)
283283
return E_HC_NOT_INITIALISED;
284284

@@ -309,7 +309,7 @@ try
309309

310310
if (call == nullptr)
311311
{
312-
auto httpSingleton = get_http_singleton(true);
312+
auto httpSingleton = get_http_singleton();
313313
if (nullptr == httpSingleton)
314314
return E_HC_NOT_INITIALISED;
315315

@@ -351,7 +351,7 @@ try
351351
{
352352
if (call == nullptr)
353353
{
354-
auto httpSingleton = get_http_singleton(true);
354+
auto httpSingleton = get_http_singleton();
355355
if (nullptr == httpSingleton)
356356
return E_HC_NOT_INITIALISED;
357357

@@ -383,7 +383,7 @@ try
383383

384384
if (call == nullptr)
385385
{
386-
auto httpSingleton = get_http_singleton(true);
386+
auto httpSingleton = get_http_singleton();
387387
if (nullptr == httpSingleton)
388388
return E_HC_NOT_INITIALISED;
389389

@@ -407,7 +407,7 @@ try
407407
{
408408
if (call == nullptr)
409409
{
410-
auto httpSingleton = get_http_singleton(true);
410+
auto httpSingleton = get_http_singleton();
411411
if (nullptr == httpSingleton)
412412
return E_HC_NOT_INITIALISED;
413413

@@ -439,7 +439,7 @@ try
439439

440440
if (call == nullptr)
441441
{
442-
auto httpSingleton = get_http_singleton(true);
442+
auto httpSingleton = get_http_singleton();
443443
if (nullptr == httpSingleton)
444444
return E_HC_NOT_INITIALISED;
445445

@@ -467,7 +467,7 @@ try
467467

468468
if (call == nullptr)
469469
{
470-
auto httpSingleton = get_http_singleton(true);
470+
auto httpSingleton = get_http_singleton();
471471
if (nullptr == httpSingleton)
472472
return E_HC_NOT_INITIALISED;
473473

@@ -490,7 +490,7 @@ try
490490
{
491491
if (call == nullptr)
492492
{
493-
auto httpSingleton = get_http_singleton(true);
493+
auto httpSingleton = get_http_singleton();
494494
if (nullptr == httpSingleton)
495495
return E_HC_NOT_INITIALISED;
496496

Source/Mock/lhc_mock.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bool Mock_Internal_HCHttpCallPerformAsync(
3838
_In_ HCCallHandle originalCall
3939
)
4040
{
41-
auto httpSingleton = get_http_singleton(false);
41+
auto httpSingleton = get_http_singleton();
4242
if (nullptr == httpSingleton)
4343
{
4444
return false;

Source/Mock/mock_publics.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ STDAPI HCMockCallCreate(
1616
return E_INVALIDARG;
1717
}
1818

19-
auto httpSingleton = get_http_singleton(true);
19+
auto httpSingleton = get_http_singleton();
2020
if (nullptr == httpSingleton)
2121
{
2222
return E_HC_NOT_INITIALISED;
@@ -46,7 +46,7 @@ try
4646
return E_INVALIDARG;
4747
}
4848

49-
auto httpSingleton = get_http_singleton(true);
49+
auto httpSingleton = get_http_singleton();
5050
if (nullptr == httpSingleton)
5151
{
5252
return E_HC_NOT_INITIALISED;
@@ -99,7 +99,7 @@ STDAPI HCMockRemoveMock(
9999
)
100100
try
101101
{
102-
auto httpSingleton = get_http_singleton(false);
102+
auto httpSingleton = get_http_singleton();
103103
if (nullptr == httpSingleton)
104104
{
105105
return E_HC_NOT_INITIALISED;
@@ -126,7 +126,7 @@ STDAPI
126126
HCMockClearMocks() noexcept
127127
try
128128
{
129-
auto httpSingleton = get_http_singleton(true);
129+
auto httpSingleton = get_http_singleton();
130130
if (nullptr == httpSingleton)
131131
return E_HC_NOT_INITIALISED;
132132

0 commit comments

Comments
 (0)