Skip to content

Application Crash When Using http_client Inside A Loop #1014

@hnn2206

Description

@hnn2206

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions