git.delta.rocks / jrsonnet / refs/commits / aeb8b35c0ae2

difftreelog

fix workaround TLS issues on process termination

Yaroslav Bolyukin2023-06-14parent: #1e2e6e2.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
7979
80impl Drop for IStr {80impl Drop for IStr {
81 fn drop(&mut self) {81 fn drop(&mut self) {
82 #[cold]
83 #[inline(never)]
84 fn unpool(inner: &Inner) {
85 // May fail on program termination
86 let res = POOL.try_with(|pool| pool.borrow_mut().remove(inner));
87 if res.is_ok() {
88 debug_assert_eq!(Inner::strong_count(inner), 1);
89 }
90 }
91 // First reference - current object, second - POOL
92 if Inner::strong_count(&self.0) <= 2 {82 maybe_unpool(&self.0)
93 unpool(&self.0);
94 }
95 }83 }
96}84}
9785
161 }149 }
162}150}
163151
164impl Drop for IBytes {152impl Drop for IBytes {
153 fn drop(&mut self) {
154 maybe_unpool(&self.0)
155 }
156}
157
165 fn drop(&mut self) {158fn maybe_unpool(inner: &Inner) {
166 #[cold]159 #[cold]
167 #[inline(never)]160 #[inline(never)]
168 fn unpool(inner: &Inner) {161 fn unpool(inner: &Inner) {
169 // May fail on program termination162 // May fail on program termination
170 let res = POOL.try_with(|pool| pool.borrow_mut().remove(inner));163 let _ = POOL.try_with(|pool| {
171 if res.is_ok() {164 let mut pool = pool.borrow_mut();
172 debug_assert_eq!(Inner::strong_count(inner), 1);165
173 }166 if pool.remove(inner).is_none() {
167 // On some platforms (i.e i686-windows), try_with will not fail after TLS
168 // destructor is called, but instead re-initialize the TLS with the empty pool.
169 // Allow non-pooled Drop in this case.
170 // https://github.com/CertainLach/jrsonnet/issues/98#issuecomment-1591624016
171 //
172 // However, if pool is not empty, most likely this is issue #113, and then I don't
173 // have any explainations for now.
174 assert!(pool.is_empty(), "received an unpooled string not during the program termination, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");
175 }
176 });
174 }177 }
175 // First reference - current object, second - POOL178 // First reference - current object, second - POOL
176 if Inner::strong_count(&self.0) <= 2 {179 if Inner::strong_count(&inner) <= 2 {
177 unpool(&self.0);180 unpool(&inner);
178 }181 }
179 }182}
180}
181183
182impl fmt::Debug for IBytes {184impl fmt::Debug for IBytes {
183 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {