@@ -25,19 +25,30 @@ WSLAUserSessionImpl::~WSLAUserSessionImpl()
2525{
2626 // Manually signal the VM termination events. This prevents being stuck on an API call that holds the VM lock.
2727 {
28- std::lock_guard lock (m_wslaSessionsLock );
28+ std::lock_guard lock (m_lock );
2929
30- for (auto * e : m_wslaSessions )
30+ for (auto * e : m_virtualMachines )
3131 {
32- // e->OnSessionTerminating();
32+ e->OnSessionTerminating ();
3333 }
3434 }
35+
36+ // TODO: Signal all sessions about user session termination.
37+ /* {
38+ std::lock_guard lock(m_wslaSessionsLock);
39+
40+ for (auto e : m_wslaSessions)
41+ {
42+ e->OnUserSessionTerminating();
43+ }
44+ } */
3545}
3646
3747void WSLAUserSessionImpl::OnWslaSessionTerminated (WSLASession* session)
3848{
3949 std::lock_guard lock (m_wslaSessionsLock);
40- auto pred = [session](const auto * e) { return session == e; };
50+ // Fix: Compare raw pointer from ComPtr with the given session pointer
51+ auto pred = [session](const Microsoft::WRL::ComPtr<WSLASession>& e) { return e.Get () == session; };
4152
4253 // Remove any stale session reference.
4354 m_wslaSessions.erase (std::remove_if (m_wslaSessions.begin (), m_wslaSessions.end (), pred), m_wslaSessions.end ());
@@ -55,6 +66,29 @@ HRESULT wsl::windows::service::wsla::WSLAUserSessionImpl::CreateSession(
5566
5667 // session->Start();
5768 THROW_IF_FAILED (session.CopyTo (__uuidof (IWSLASession), (void **)WslaSession));
69+ return S_OK;
70+ }
71+
72+ void WSLAUserSessionImpl::OnVmTerminated (WSLAVirtualMachine* machine)
73+ {
74+ std::lock_guard lock (m_lock);
75+ auto pred = [machine](const auto * e) { return machine == e; };
76+
77+ // Remove any stale VM reference.
78+ m_virtualMachines.erase (std::remove_if (m_virtualMachines.begin (), m_virtualMachines.end (), pred), m_virtualMachines.end ());
79+ }
80+
81+ HRESULT WSLAUserSessionImpl::CreateVirtualMachine (const VIRTUAL_MACHINE_SETTINGS* Settings, IWSLAVirtualMachine** VirtualMachine)
82+ {
83+ auto vm = wil::MakeOrThrow<WSLAVirtualMachine>(*Settings, GetUserSid (), this );
84+
85+ {
86+ std::lock_guard lock (m_lock);
87+ m_virtualMachines.emplace_back (vm.Get ());
88+ }
89+
90+ vm->Start ();
91+ THROW_IF_FAILED (vm.CopyTo (__uuidof (IWSLAVirtualMachine), (void **)VirtualMachine));
5892
5993 return S_OK;
6094}
87121
88122 return session->CreateSession (Settings, VmSettings, WslaSession);
89123}
90- CATCH_RETURN ();
124+ CATCH_RETURN ();
125+
126+ HRESULT wsl::windows::service::wsla::WSLAUserSession::CreateVirtualMachine (const VIRTUAL_MACHINE_SETTINGS* Settings, IWSLAVirtualMachine** VirtualMachine)
127+ try
128+ {
129+ auto session = m_session.lock ();
130+ RETURN_HR_IF (RPC_E_DISCONNECTED, !session);
131+
132+ return session->CreateVirtualMachine (Settings, VirtualMachine);
133+ }
134+ CATCH_RETURN ();
0 commit comments