diff options
author | mukesh agrawal <quiche@google.com> | 2016-11-15 19:45:18 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-11-15 19:45:18 +0000 |
commit | c95b2cee999918ee5eacf52e70e55a47c78d7b5c (patch) | |
tree | 05757d3299dab1f296b9714202182c0549fe8cbc | |
parent | 484b3de9387971b1a99adc7ec23f6d34c001d23d (diff) | |
parent | 0568dbda87117efcb2a739ff49cdd1de3ed7f886 (diff) | |
download | wifilogd-c95b2cee999918ee5eacf52e70e55a47c78d7b5c.tar.gz |
MainLoop: improve error handling am: fd59221459 am: 8fbb6736ff am: 02b8923766android-vts-8.0_r9android-vts-8.0_r8android-vts-8.0_r7android-vts-8.0_r6android-vts-8.0_r2android-vts-8.0_r13android-vts-8.0_r12android-vts-8.0_r11android-vts-8.0_r10android-vts-8.0_r1android-security-8.0.0_r54android-security-8.0.0_r53android-security-8.0.0_r52android-cts-8.0_r9android-cts-8.0_r8android-cts-8.0_r7android-cts-8.0_r6android-cts-8.0_r5android-cts-8.0_r4android-cts-8.0_r3android-cts-8.0_r26android-cts-8.0_r25android-cts-8.0_r24android-cts-8.0_r23android-cts-8.0_r22android-cts-8.0_r21android-cts-8.0_r20android-cts-8.0_r2android-cts-8.0_r19android-cts-8.0_r18android-cts-8.0_r17android-cts-8.0_r16android-cts-8.0_r15android-cts-8.0_r14android-cts-8.0_r13android-cts-8.0_r12android-cts-8.0_r11android-cts-8.0_r10android-cts-8.0_r1android-8.0.0_r9android-8.0.0_r7android-8.0.0_r51android-8.0.0_r50android-8.0.0_r49android-8.0.0_r48android-8.0.0_r47android-8.0.0_r46android-8.0.0_r45android-8.0.0_r44android-8.0.0_r43android-8.0.0_r42android-8.0.0_r41android-8.0.0_r40android-8.0.0_r4android-8.0.0_r39android-8.0.0_r38android-8.0.0_r37android-8.0.0_r36android-8.0.0_r35android-8.0.0_r32android-8.0.0_r31android-8.0.0_r30android-8.0.0_r3android-8.0.0_r29android-8.0.0_r28android-8.0.0_r2android-8.0.0_r17android-8.0.0_r16android-8.0.0_r15android-8.0.0_r13android-8.0.0_r12android-8.0.0_r11android-8.0.0_r10android-8.0.0_r1security-oc-releaseoreo-vts-releaseoreo-security-releaseoreo-releaseoreo-r6-releaseoreo-r5-releaseoreo-r4-releaseoreo-r3-releaseoreo-r2-releaseoreo-devoreo-cts-release
am: 0568dbda87
Change-Id: Ie76fc927106cbbc692249ca67ddd19a6511016f7
-rw-r--r-- | main_loop.cpp | 20 | ||||
-rw-r--r-- | main_loop.h | 2 | ||||
-rw-r--r-- | tests/main_loop_unittest.cpp | 9 |
3 files changed, 28 insertions, 3 deletions
diff --git a/main_loop.cpp b/main_loop.cpp index 62bed31..8ad030a 100644 --- a/main_loop.cpp +++ b/main_loop.cpp @@ -30,6 +30,8 @@ namespace wifilogd { namespace { constexpr auto kMainBufferSizeBytes = 128 * 1024; +// TODO(b/32840641): Tune the sleep time. +constexpr auto kTransientErrorSleepTimeNsec = 100 * 1000; // 100 usec } MainLoop::MainLoop(const std::string& socket_name) @@ -53,8 +55,7 @@ void MainLoop::RunOnce() { std::tie(datagram_len, err) = os_->ReceiveDatagram(sock_fd_, input_buf.data(), input_buf.size()); if (err) { - // TODO(b/32098735): Increment stats counter. - // TODO(b/32481888): Improve error handling. + ProcessError(err); return; } @@ -67,5 +68,20 @@ void MainLoop::RunOnce() { Os::kInvalidFd); } +// Private methods below. + +void MainLoop::ProcessError(Os::Errno err) { + if (err == EINTR || err == ENOMEM) { + // TODO(b/32098735): Increment stats counter. + os_->Nanosleep(kTransientErrorSleepTimeNsec); + return; + } + + // Any other error is unexpected, and assumed to be non-recoverable. + // (If, e.g., our socket is in a bad state, then we won't be able to receive + // any new log messages.) + LOG(FATAL) << "Unexpected error: " << std::strerror(err); +} + } // namespace wifilogd } // namespace android diff --git a/main_loop.h b/main_loop.h index c5a4b48..53c42f8 100644 --- a/main_loop.h +++ b/main_loop.h @@ -39,6 +39,8 @@ class MainLoop { void RunOnce(); private: + void ProcessError(Os::Errno err); + std::unique_ptr<Os> os_; std::unique_ptr<CommandProcessor> command_processor_; // We use an int, rather than a unique_fd, because the file diff --git a/tests/main_loop_unittest.cpp b/tests/main_loop_unittest.cpp index db04027..b6c1141 100644 --- a/tests/main_loop_unittest.cpp +++ b/tests/main_loop_unittest.cpp @@ -109,9 +109,10 @@ TEST_F(MainLoopTest, RunOnceLimitsMaxSizeReportedToCommandProcessor) { main_loop_->RunOnce(); } -TEST_F(MainLoopTest, RunOnceDoesNotPassDataToCommandProcessorOnError) { +TEST_F(MainLoopTest, RunOnceSleepsAndDoesNotPassDataToCommandProcessorOnError) { EXPECT_CALL(*os_, ReceiveDatagram(_, _, protocol::kMaxMessageSize)) .WillOnce(Return(std::tuple<size_t, Os::Errno>{0, EINTR})); + EXPECT_CALL(*os_, Nanosleep(_)); EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(0); main_loop_->RunOnce(); } @@ -131,5 +132,11 @@ TEST_F(MainLoopDeathTest, CtorFailureToFetchControlSocketCausesDeath) { "Failed to get control socket"); } +TEST_F(MainLoopDeathTest, RunOnceTerminatesOnUnexpectedError) { + ON_CALL(*os_, ReceiveDatagram(_, _, protocol::kMaxMessageSize)) + .WillByDefault(Return(std::tuple<size_t, Os::Errno>{0, EFAULT})); + EXPECT_DEATH(main_loop_->RunOnce(), "Unexpected error"); +} + } // namespace wifilogd } // namespace android |