Skip to content
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

Application Crash When Using http_client Inside A Loop #1014

Open
hnn2206 opened this issue Jan 4, 2019 · 1 comment
Open

Application Crash When Using http_client Inside A Loop #1014

hnn2206 opened this issue Jan 4, 2019 · 1 comment

Comments

@hnn2206
Copy link

hnn2206 commented Jan 4, 2019

I encounter an issue and hopefully someone would be able to help. I use http_client to do a POST to a web service inside a loop. After a while the application crashes. Putting try/catch does not help. Here is the code:
while (itr != m_PollMsgList.end() && !fShouldStop)
{
pMsg = (itr);
if (pMsg)
{
try
{
Sleep(10);
SMGKernel.Sender.GetReply(pMsg);
}
catch (...)
{
}
}
}
int CSMGSender::GetReply(PollMessage
pMsg)
{
if (pMsg == NULL)
{
return 0;
}

int nErrorCode, *pErrorCode;
char szMsg[1025], szJobId[512];
json::value obj;

memset(szMsg, 0x0, sizeof(szMsg));
memset(szJobId, 0x0, sizeof(szJobId));

if (pMsg->UseJobId)
{
	wsprintf(szJobId, "%s", pMsg->SMGJobId);
}
else
{
	wsprintf(szJobId, "%d", pMsg->StlFormId);
}

pErrorCode = &nErrorCode;
nErrorCode = -1;

try
{
	http_request request(methods::POST);
	http_client_config client_config;
	credentials cred(to_string_t(m_strUser), to_string_t(m_strOrigToken));
	client_config.set_credentials(cred);

	request.headers().add(L"Authorization", to_string_t(m_strAuthToken));

	obj[L"cmd"] = json::value(U("reply"));
	obj[L"id"] = json::value(to_string_t(szJobId));
	obj[L"type"] = json::value(pMsg->Provider).as_integer();
	obj[L"device"] = json::value(to_string_t(pMsg->Device));
	request.set_body(obj);
	
	m_PollerClient->request(request)
	.then([pErrorCode](http_response response) -> pplx::task<json::value>
	{
		// Get status code.
		status_code respcode = response.status_code();
		*pErrorCode = respcode;
		if ((respcode != status_codes::OK))
		{
			return pplx::task_from_result(json::value());
		}
		return response.extract_json(true);
	})
	.then([pErrorCode, pMsg](pplx::task<json::value> previousTask)
	{
		try
		{
			if (*pErrorCode == status_codes::OK)
			{
				const json::value & v = previousTask.get();
				if (v.has_field(L"status"))
				{
					string_t status = v.at(L"status").as_string();
					if (status.compare(L"OK") == 0)
					{
						*pErrorCode = 0;
						if (v.has_field(L"comment"))
						{
							json::array commentList = v.at(L"comment").as_array();
							int nCount = commentList.size();
							bool bHasReplies = false;
							for (int i = 0; i < nCount; i++)
							{
								string_t reply = commentList[i].as_string();
								if (reply.size() > 0)
								{
									SMGKernel.Sender.ProcessSMGResponse(pMsg->StlFormId, pMsg->SMGJobId, pMsg->IsOrder, pMsg->Device, pMsg->Provider, reply);
									bHasReplies = true;
								}
							}

							if (bHasReplies && pMsg->Provider == CStlSMGServer::PROVIDER::PROV_TWISTLE)
							{
								// If provider is Twistle and there are any replies. Mark msg as expired to prevent getting the same replies in the next poll.
								pMsg->Expired = true;
							}
						}
					}
				}
			}
		}
		catch (http_exception const & e)
		{
			*pErrorCode = -2;
		}
	})
	.wait();
}
catch (const std::system_error &ex)
{
	
}
catch (const std::exception &e)
{
	
}
catch (http_exception const & e)
{
	*pErrorCode = -2;
}
catch (...)
{
	
}

return nErrorCode;
@BillyONeal
Copy link
Member

What version are you using? What call stack do you get when it crashes?

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

No branches or pull requests

2 participants