1
0
Fork 0
forked from suyu/suyu

Merge pull request #3162 from bunnei/fix-shared-ptr-crash

kernel: Fix reference management for client/server session.
This commit is contained in:
Fernando Sahmkow 2019-11-26 07:13:04 -04:00 committed by GitHub
commit 59484442a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 20 deletions

View file

@ -16,20 +16,18 @@ ClientSession::ClientSession(KernelCore& kernel) : Object{kernel} {}
ClientSession::~ClientSession() { ClientSession::~ClientSession() {
// This destructor will be called automatically when the last ClientSession handle is closed by // This destructor will be called automatically when the last ClientSession handle is closed by
// the emulated application. // the emulated application.
if (parent->server) { if (auto server = parent->server.lock()) {
parent->server->ClientDisconnected(); server->ClientDisconnected();
} }
parent->client = nullptr;
} }
ResultCode ClientSession::SendSyncRequest(Thread* thread) { ResultCode ClientSession::SendSyncRequest(Thread* thread) {
// Keep ServerSession alive until we're done working with it.
if (parent->server == nullptr)
return ERR_SESSION_CLOSED_BY_REMOTE;
// Signal the server session that new data is available // Signal the server session that new data is available
return parent->server->HandleSyncRequest(SharedFrom(thread)); if (auto server = parent->server.lock()) {
return server->HandleSyncRequest(SharedFrom(thread));
}
return ERR_SESSION_CLOSED_BY_REMOTE;
} }
} // namespace Kernel } // namespace Kernel

View file

@ -31,8 +31,6 @@ ServerSession::~ServerSession() {
if (parent->port) { if (parent->port) {
parent->port->ConnectionClosed(); parent->port->ConnectionClosed();
} }
parent->server = nullptr;
} }
ResultVal<std::shared_ptr<ServerSession>> ServerSession::Create(KernelCore& kernel, ResultVal<std::shared_ptr<ServerSession>> ServerSession::Create(KernelCore& kernel,
@ -46,11 +44,13 @@ ResultVal<std::shared_ptr<ServerSession>> ServerSession::Create(KernelCore& kern
} }
bool ServerSession::ShouldWait(const Thread* thread) const { bool ServerSession::ShouldWait(const Thread* thread) const {
// Closed sessions should never wait, an error will be returned from svcReplyAndReceive.
if (parent->client == nullptr)
return false;
// Wait if we have no pending requests, or if we're currently handling a request. // Wait if we have no pending requests, or if we're currently handling a request.
if (auto client = parent->client.lock()) {
return pending_requesting_threads.empty() || currently_handling != nullptr; return pending_requesting_threads.empty() || currently_handling != nullptr;
}
// Closed sessions should never wait, an error will be returned from svcReplyAndReceive.
return {};
} }
void ServerSession::Acquire(Thread* thread) { void ServerSession::Acquire(Thread* thread) {
@ -192,9 +192,9 @@ ServerSession::SessionPair ServerSession::CreateSessionPair(KernelCore& kernel,
std::shared_ptr<ClientSession> client_session = std::make_shared<ClientSession>(kernel); std::shared_ptr<ClientSession> client_session = std::make_shared<ClientSession>(kernel);
client_session->name = name + "_Client"; client_session->name = name + "_Client";
std::shared_ptr<Session> parent(new Session); std::shared_ptr<Session> parent = std::make_shared<Session>();
parent->client = client_session.get(); parent->client = client_session;
parent->server = server_session.get(); parent->server = server_session;
parent->port = std::move(port); parent->port = std::move(port);
client_session->parent = parent; client_session->parent = parent;

View file

@ -20,8 +20,8 @@ class ServerSession;
*/ */
class Session final { class Session final {
public: public:
ClientSession* client = nullptr; ///< The client endpoint of the session. std::weak_ptr<ClientSession> client; ///< The client endpoint of the session.
ServerSession* server = nullptr; ///< The server endpoint of the session. std::weak_ptr<ServerSession> server; ///< The server endpoint of the session.
std::shared_ptr<ClientPort> port; ///< The port that this session is associated with (optional). std::shared_ptr<ClientPort> port; ///< The port that this session is associated with (optional).
}; };
} // namespace Kernel } // namespace Kernel