lib/nfs/Connection: call nfs_destroy_context() only in the destructor

This simplifies the lifetime of the nfs_context object.
This commit is contained in:
Max Kellermann 2024-05-06 18:03:32 +02:00
parent 90a44a0c07
commit 48fe8666c9
2 changed files with 15 additions and 14 deletions

View File

@ -190,8 +190,10 @@ NfsConnection::~NfsConnection() noexcept
assert(callbacks.IsEmpty()); assert(callbacks.IsEmpty());
assert(deferred_close.empty()); assert(deferred_close.empty());
if (context != nullptr) if (context != nullptr) {
DestroyContext(); PrepareDestroyContext();
nfs_destroy_context(context);
}
} }
void void
@ -369,7 +371,7 @@ NfsConnection::CancelAndClose(struct nfsfh *fh, NfsCallback &callback) noexcept
} }
void void
NfsConnection::DestroyContext() noexcept NfsConnection::PrepareDestroyContext() noexcept
{ {
assert(GetEventLoop().IsInside()); assert(GetEventLoop().IsInside());
assert(context != nullptr); assert(context != nullptr);
@ -396,8 +398,10 @@ NfsConnection::DestroyContext() noexcept
c.PrepareDestroyContext(); c.PrepareDestroyContext();
}); });
nfs_destroy_context(context); #ifndef NDEBUG
context = nullptr; assert(in_destroy);
in_destroy = false;
#endif
} }
inline void inline void
@ -493,7 +497,7 @@ NfsConnection::OnSocketReady(unsigned flags) noexcept
if (!was_mounted && mount_state >= MountState::FINISHED) { if (!was_mounted && mount_state >= MountState::FINISHED) {
if (postponed_mount_error) { if (postponed_mount_error) {
DestroyContext(); PrepareDestroyContext();
BroadcastMountError(std::move(postponed_mount_error)); BroadcastMountError(std::move(postponed_mount_error));
} else if (result == 0) } else if (result == 0)
BroadcastMountSuccess(); BroadcastMountSuccess();
@ -503,7 +507,7 @@ NfsConnection::OnSocketReady(unsigned flags) noexcept
auto e = NfsClientError(context, "NFS connection has failed"); auto e = NfsClientError(context, "NFS connection has failed");
BroadcastError(std::make_exception_ptr(e)); BroadcastError(std::make_exception_ptr(e));
DestroyContext(); PrepareDestroyContext();
} else if (nfs_get_fd(context) < 0) { } else if (nfs_get_fd(context) < 0) {
/* this happens when rpc_reconnect_requeue() is called /* this happens when rpc_reconnect_requeue() is called
after the connection broke, but autoreconnect was after the connection broke, but autoreconnect was
@ -513,7 +517,7 @@ NfsConnection::OnSocketReady(unsigned flags) noexcept
BroadcastError(std::make_exception_ptr(e)); BroadcastError(std::make_exception_ptr(e));
DestroyContext(); PrepareDestroyContext();
} }
assert(context == nullptr || nfs_get_fd(context) >= 0); assert(context == nullptr || nfs_get_fd(context) >= 0);
@ -581,10 +585,7 @@ NfsConnection::MountInternal()
if (nfs_mount_async(context, server.c_str(), export_name.c_str(), if (nfs_mount_async(context, server.c_str(), export_name.c_str(),
MountCallback, this) != 0) { MountCallback, this) != 0) {
mount_state = MountState::FINISHED; mount_state = MountState::FINISHED;
auto e = NfsClientError(context, "nfs_mount_async() failed"); throw NfsClientError(context, "nfs_mount_async() failed");
nfs_destroy_context(context);
context = nullptr;
throw e;
} }
ScheduleSocket(); ScheduleSocket();
@ -634,7 +635,7 @@ NfsConnection::OnMountTimeout() noexcept
assert(mount_state == MountState::WAITING); assert(mount_state == MountState::WAITING);
mount_state = MountState::FINISHED; mount_state = MountState::FINISHED;
DestroyContext(); PrepareDestroyContext();
BroadcastMountError(std::make_exception_ptr(std::runtime_error("Mount timeout"))); BroadcastMountError(std::make_exception_ptr(std::runtime_error("Mount timeout")));
} }

View File

@ -189,7 +189,7 @@ protected:
virtual void OnNfsConnectionError(std::exception_ptr e) noexcept = 0; virtual void OnNfsConnectionError(std::exception_ptr e) noexcept = 0;
private: private:
void DestroyContext() noexcept; void PrepareDestroyContext() noexcept;
/** /**
* Wrapper for nfs_close_async(). * Wrapper for nfs_close_async().