Skip to content

Commit 07230f2

Browse files
committed
refactor: rename ProxyClient<Thread>::m_cleanup_it
Rename `ProxyClient<Thread>` `m_cleanup_it` member to `m_disconnect_cb`. There is no change in behavor in this commit. The change is just being made so a bugfix in an upcoming commit will be more obvious. The `m_cleanup_it` name was a bad name because it didn't make it clear that this cleanup function only runs on sudden disconnects, not clean shutdowns. It was also confusing because this is a member of ProxyClient object, which has its own cleanup functions, but this doesn't actually refer to one of those functions, it refers to a cleanup function in an entirely different object (the Connection object).
1 parent 0d986ff commit 07230f2

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

include/mp/proxy-io.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ struct ProxyClient<Thread> : public ProxyClientBase<Thread, ::capnp::Void>
6666
ProxyClient(const ProxyClient&) = delete;
6767
~ProxyClient();
6868

69-
void setCleanup(const std::function<void()>& fn);
70-
71-
//! Cleanup function to run when the connection is closed. If the Connection
72-
//! gets destroyed before this ProxyClient<Thread> object, this cleanup
73-
//! callback lets it destroy this object and remove its entry in the
74-
//! thread's request_threads or callback_threads map (after resetting
75-
//! m_cleanup_it so the destructor does not try to access it). But if this
76-
//! object gets destroyed before the Connection, there's no need to run the
77-
//! cleanup function and the destructor will unregister it.
78-
std::optional<CleanupIt> m_cleanup_it;
69+
void setDisconnectCallback(const std::function<void()>& fn);
70+
71+
//! Reference to callback function that is run if there is a sudden
72+
//! disconnect and the Connection object is destroyed before this
73+
//! ProxyClient<Thread> object. The callback will destroy this object and
74+
//! remove its entry from the thread's request_threads or callback_threads
75+
//! map. It will also reset m_disconnect_cb so the destructor does not
76+
//! access it. In the normal case where there is no sudden disconnect, the
77+
//! destructor will unregister m_disconnect_cb so the callback is never run.
78+
std::optional<CleanupIt> m_disconnect_cb;
7979
};
8080

8181
template <>

src/mp/proxy.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,16 @@ std::tuple<ConnThread, bool> SetThread(ConnThreads& threads, std::mutex& mutex,
311311
thread = threads.emplace(
312312
std::piecewise_construct, std::forward_as_tuple(connection),
313313
std::forward_as_tuple(make_thread(), connection, /* destroy_connection= */ false)).first;
314-
thread->second.setCleanup([&threads, &mutex, thread] {
314+
thread->second.setDisconnectCallback([&threads, &mutex, thread] {
315315
// Note: it is safe to use the `thread` iterator in this cleanup
316316
// function, because the iterator would only be invalid if the map entry
317317
// was removed, and if the map entry is removed the ProxyClient<Thread>
318318
// destructor unregisters the cleanup.
319319

320320
// Connection is being destroyed before thread client is, so reset
321-
// thread client m_cleanup_it member so thread client destructor does not
321+
// thread client m_disconnect_cb member so thread client destructor does not
322322
// try unregister this callback after connection is destroyed.
323-
thread->second.m_cleanup_it.reset();
323+
thread->second.m_disconnect_cb.reset();
324324
// Remove connection pointer about to be destroyed from the map
325325
const std::unique_lock<std::mutex> lock(mutex);
326326
threads.erase(thread);
@@ -333,16 +333,16 @@ ProxyClient<Thread>::~ProxyClient()
333333
// If thread is being destroyed before connection is destroyed, remove the
334334
// cleanup callback that was registered to handle the connection being
335335
// destroyed before the thread being destroyed.
336-
if (m_cleanup_it) {
337-
m_context.connection->removeSyncCleanup(*m_cleanup_it);
336+
if (m_disconnect_cb) {
337+
m_context.connection->removeSyncCleanup(*m_disconnect_cb);
338338
}
339339
}
340340

341-
void ProxyClient<Thread>::setCleanup(const std::function<void()>& fn)
341+
void ProxyClient<Thread>::setDisconnectCallback(const std::function<void()>& fn)
342342
{
343343
assert(fn);
344-
assert(!m_cleanup_it);
345-
m_cleanup_it = m_context.connection->addSyncCleanup(fn);
344+
assert(!m_disconnect_cb);
345+
m_disconnect_cb = m_context.connection->addSyncCleanup(fn);
346346
}
347347

348348
ProxyServer<Thread>::ProxyServer(ThreadContext& thread_context, std::thread&& thread)

0 commit comments

Comments
 (0)