Commit a871442
MB-48816: Avoid unsafe use of cookie from background tasks
Previously, StatCheckpointTask and StatDCPTask immediately wrote
responses when collecting stats while on a background thread.
TSAN reported this as unsafe; no locks prevent potential racing with a
frontend thread manipulating the cookie.
Change both tasks to accumulate task values, but leave the frontend
thread to actually write the responses when it resumes the
ewouldblock'ed operation.
TSAN Report:
WARNING: ThreadSanitizer: data race (pid=24371)
Read of size 8 at 0x7b54000a2df0 by thread T62:
#0 Cookie::getHeader() const kv_engine/daemon/cookie.cc:201 (memcached+0x6508ac)
#1 append_stats kv_engine/daemon/protocol/mcbp/stats_context.cc:95 (memcached+0x71fd6c)
....
#19 void StatCollector::addStat<cb::stats::Key, unsigned long const&>(cb::stats::Key&&, unsigned long const&) const ../kv_engine/include/statistics/collector.h:336 (memcached+0x7e50e5)
#20 EventuallyPersistentEngine::addAggregatedProducerStats(BucketStatCollector const&, ConnCounter const&) kv_engine/engines/ep/src/ep_engine.cc:4038 (memcached+0x7e50e5)
#21 EventuallyPersistentEngine::doDcpStatsInner(CookieIface const*, std::function<void (std::basic_string_view<char, std::char_traits<char> >, std::basic_string_view<char, std::char_traits<char> >, void const*)> const&, std::basic_string_view<char, std::char_traits<char> >) kv_engine/engines/ep/src/ep_engine.cc:4030 (memcached+0x81bd05)
Previous write of size 8 at 0x7b54000a2df0 by thread T21 (mutexes: write M3843):
#0 Cookie::setPacket(cb::mcbp::Header const&, bool) kv_engine/daemon/cookie.cc:186 (memcached+0x65080e)
#1 Cookie::preserveRequest() kv_engine/daemon/cookie.h:225 (memcached+0x696aa7)
#2 Connection::executeCommandPipeline() kv_engine/daemon/connection.cc:581 (memcached+0x696aa7)
#3 Connection::executeCommandsCallback() kv_engine/daemon/connection.cc:793 (memcached+0x696be8)
#4 Connection::rw_callback(bufferevent*, void*) kv_engine/daemon/connection.cc:942 (memcached+0x697851)
#5 bufferevent_run_deferred_callbacks_unlocked /home/couchbase/jenkins/workspace/cbdeps-platform-build-old/deps/packages/build/libevent/libevent-prefix/src/libevent/bufferevent.c:208 (libevent_core-2.1.so.7+0xf71d)
#6 folly::EventBase::loopBody(int, bool) folly/io/async/EventBase.cpp:397 (memcached+0xfc9b52)
#7 folly::EventBase::loop() folly/io/async/EventBase.cpp:315 (memcached+0xfcb06b)
#8 folly::EventBase::loopForever() folly/io/async/EventBase.cpp:538 (memcached+0xfcb06b)
#9 worker_libevent kv_engine/daemon/thread.cc:115 (memcached+0x6c16af)
#10 CouchbaseThread::run() platform/src/cb_pthreads.cc:51 (memcached+0xf217d5)
#11 platform_thread_wrap platform/src/cb_pthreads.cc:64 (memcached+0xf217d5)
Change-Id: I3fbd8d51e174a7d19c5cb608a969795e445b8e86
Reviewed-on: http://review.couchbase.org/c/kv_engine/+/163709
Tested-by: Build Bot <[email protected]>
Reviewed-by: Dave Rigby <[email protected]>1 parent 8ae3b4f commit a871442
File tree
5 files changed
+220
-45
lines changed- engines/ep
- src
5 files changed
+220
-45
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
| 295 | + | |
295 | 296 | | |
296 | 297 | | |
297 | 298 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1848 | 1848 | | |
1849 | 1849 | | |
1850 | 1850 | | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
| 1859 | + | |
| 1860 | + | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
1851 | 1876 | | |
1852 | 1877 | | |
1853 | 1878 | | |
| |||
3756 | 3781 | | |
3757 | 3782 | | |
3758 | 3783 | | |
3759 | | - | |
3760 | | - | |
| 3784 | + | |
3761 | 3785 | | |
3762 | 3786 | | |
3763 | 3787 | | |
3764 | 3788 | | |
3765 | | - | |
3766 | | - | |
3767 | | - | |
3768 | | - | |
| 3789 | + | |
3769 | 3790 | | |
3770 | | - | |
| 3791 | + | |
| 3792 | + | |
3771 | 3793 | | |
3772 | | - | |
3773 | | - | |
3774 | | - | |
3775 | | - | |
| 3794 | + | |
| 3795 | + | |
| 3796 | + | |
| 3797 | + | |
3776 | 3798 | | |
3777 | 3799 | | |
3778 | 3800 | | |
| |||
3785 | 3807 | | |
3786 | 3808 | | |
3787 | 3809 | | |
3788 | | - | |
3789 | | - | |
3790 | | - | |
3791 | | - | |
3792 | | - | |
3793 | 3810 | | |
3794 | 3811 | | |
3795 | 3812 | | |
| |||
3799 | 3816 | | |
3800 | 3817 | | |
3801 | 3818 | | |
3802 | | - | |
3803 | | - | |
3804 | | - | |
3805 | | - | |
| 3819 | + | |
| 3820 | + | |
| 3821 | + | |
3806 | 3822 | | |
3807 | | - | |
| 3823 | + | |
3808 | 3824 | | |
3809 | 3825 | | |
3810 | | - | |
| 3826 | + | |
3811 | 3827 | | |
3812 | 3828 | | |
3813 | 3829 | | |
| |||
4034 | 4050 | | |
4035 | 4051 | | |
4036 | 4052 | | |
4037 | | - | |
| 4053 | + | |
4038 | 4054 | | |
4039 | 4055 | | |
4040 | 4056 | | |
4041 | 4057 | | |
4042 | 4058 | | |
4043 | 4059 | | |
4044 | 4060 | | |
4045 | | - | |
4046 | | - | |
4047 | | - | |
| 4061 | + | |
4048 | 4062 | | |
4049 | 4063 | | |
4050 | 4064 | | |
4051 | | - | |
| 4065 | + | |
4052 | 4066 | | |
4053 | 4067 | | |
4054 | 4068 | | |
| |||
4060 | 4074 | | |
4061 | 4075 | | |
4062 | 4076 | | |
4063 | | - | |
4064 | | - | |
| 4077 | + | |
4065 | 4078 | | |
4066 | 4079 | | |
4067 | 4080 | | |
| |||
4075 | 4088 | | |
4076 | 4089 | | |
4077 | 4090 | | |
4078 | | - | |
4079 | | - | |
4080 | 4091 | | |
4081 | 4092 | | |
4082 | 4093 | | |
| |||
4085 | 4096 | | |
4086 | 4097 | | |
4087 | 4098 | | |
4088 | | - | |
4089 | | - | |
4090 | | - | |
| 4099 | + | |
| 4100 | + | |
| 4101 | + | |
4091 | 4102 | | |
4092 | 4103 | | |
4093 | 4104 | | |
| |||
4100 | 4111 | | |
4101 | 4112 | | |
4102 | 4113 | | |
4103 | | - | |
| 4114 | + | |
4104 | 4115 | | |
4105 | 4116 | | |
4106 | | - | |
| 4117 | + | |
4107 | 4118 | | |
4108 | | - | |
4109 | | - | |
4110 | 4119 | | |
4111 | 4120 | | |
4112 | 4121 | | |
| |||
4137 | 4146 | | |
4138 | 4147 | | |
4139 | 4148 | | |
4140 | | - | |
4141 | | - | |
4142 | | - | |
| 4149 | + | |
| 4150 | + | |
| 4151 | + | |
4143 | 4152 | | |
4144 | 4153 | | |
4145 | 4154 | | |
| |||
4150 | 4159 | | |
4151 | 4160 | | |
4152 | 4161 | | |
4153 | | - | |
| 4162 | + | |
4154 | 4163 | | |
4155 | 4164 | | |
4156 | | - | |
| 4165 | + | |
4157 | 4166 | | |
4158 | | - | |
4159 | | - | |
4160 | 4167 | | |
4161 | 4168 | | |
4162 | 4169 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
538 | 539 | | |
539 | 540 | | |
540 | 541 | | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
541 | 571 | | |
542 | 572 | | |
543 | 573 | | |
| |||
0 commit comments