Hi Guys,
Found some tiny app within the
net/tools/get_server_time
folder and tried its approach for sending a HTTP-request to a host. If the following code snippet (got from the found sample app):
base::MessageLoopForIO main_loop;
QuitDelegate delegate;
std::unique_ptr<net::URLFetcher> fetcher =
net::URLFetcher::Create(GURL(fetch_url.c_str()), net::URLFetcher::GET,
&delegate);
std::unique_ptr<net::URLRequestContext> url_request_context(
BuildURLRequestContext());
fetcher->SetRequestContext(
// Since there's only a single thread, there's no need to worry
// about when the URLRequestContext gets created.
// The URLFetcher will take a reference on the object, and hence
// implicitly take ownership.
new net::TrivialURLRequestContextGetter(url_request_context.get(),
main_loop.task_runner()));
fetcher->Start();
// |delegate| quits |main_loop| when the request is done.
base::RunLoop().Run();
const net::URLRequestStatus status = fetcher->GetStatus();
if (status.status() != net::URLRequestStatus::SUCCESS) {
LOG(ERROR) << __func__ << "Request failed with error code: "
<< net::ErrorToString(status.error());
}
is placed into the main() function of a single-threaded sample app (even if it's wrapped by a loop to emulate repeating requests to some HTTP-host), it works just fine (except a corresponding delegate overridden member is never called, although it still gets correct HTTP-responses just as expected). But if I place it into a dedicated function bound to the base::Timer instance (repeating timer case) being a member of a custom thread (inherited from the base::Thread type), the created request is able to run correctly just once (for the very first time only). Any further attempt of the timer to fire and run a specified closure (with the code listed above) causes the application to crash with a backtrace generated as follows:
Received signal 11 SEGV_MAPERR 000000000000
#0 0x000001452c37 base::debug::StackTrace::StackTrace()
#1 0x0000014527af base::debug::(anonymous namespace)::StackDumpSignalHandler()
#2 0x7ff316bfc390 <unknown>
#3 0x0000014a45de base::ThreadTaskRunnerHandle::Get()
#4 0x0000014a521d base::Timer::PostNewScheduledTask()
#5 0x0000014a5444 base::Timer::RunScheduledTask()
#6 0x0000014cc170 base::debug::TaskAnnotator::RunTask()
#7 0x00000146925d base::MessageLoop::RunTask()
#8 0x0000014695a8 base::MessageLoop::DeferOrRunPendingTask()
#9 0x000001469aab base::MessageLoop::DoDelayedWork()
#10 0x00000146a65d base::MessagePumpDefault::Run()
#11 0x0000014836fe base::RunLoop::Run()
#12 0x0000014a3492 base::Thread::ThreadMain()
#13 0x00000149ee63 base::(anonymous namespace)::ThreadFunc()
#14 0x7ff316bf26ba start_thread
#15 0x7ff311b123dd clone
r8: 0000000000000000 r9: 0000000000000000 r10: 00007ff2f83029d0 r11: 0000000000000206
r12: 000013aee8bcd9a0 r13: 0000000000000000 r14: 00000000000186a0 r15: 000013aee8b05a80
di: 0000000000000020 si: 0000000000000001 bp: 00007ff2f8302a50 bx: 00007ff2f8302758
dx: 0000000000000000 ax: 0000000000000000 cx: 0000000000000090 sp: 00007ff2f8302730
ip: 00000000014a45de efl: 0000000000010246 cgf: 0000000000000033 erf: 0000000000000004
trp: 000000000000000e msk: 0000000000000000 cr2: 0000000000000000
[end of stack trace]
Calling _exit(1). Core file will not be generated.Seems like a task runner for the current thread which the timer is trying to get has just disappeared after the first timer firing attempt. Any ideas on why this happened (and how to handle that correctly)?
Any either directions or thoughts are highly appreciated. Thank you.
P.S.
Specifying the custom thread spawned to use a message loop of the base::MessageLoop::TYPE_IO type as well as providing it to the
net::TrivialURLRequestContextGetter instance (instead of
base::MessageLoopForIO) prevents the URL-request from being even responded to.
BRs,
Sergey Kipet.
четверг, 28 сентября 2017 г., 17:51:51 UTC+3 пользователь Sergey Kipet написал: