Skip to content

Commit

Permalink
fix(shared): Fix Resource Stop freezing the server + remove debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Feb 21, 2024
1 parent 8558d3c commit 1370065
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
12 changes: 0 additions & 12 deletions server/src/CNodeResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,24 @@ bool CNodeResource::Start()

bool CNodeResource::Stop()
{
js::Logger::Colored << "~y~[JS] Stopping resource " << resource->GetName() << js::Logger::Endl;
IResource::Scope scope(this);

js::Logger::Colored << "~y~[JS] Emitting process exit event" << js::Logger::Endl;
node::EmitAsyncDestroy(isolate, asyncContext);
js::Logger::Colored << "~y~[JS] Resetting asyncResource" << js::Logger::Endl;
asyncResource.Reset();

js::Logger::Colored << "~y~[JS] Emitting process exit event" << js::Logger::Endl;
node::EmitProcessBeforeExit(env);
node::EmitProcessExit(env);

js::Logger::Colored << "~y~[JS] Stopping environment" << js::Logger::Endl;
node::Stop(env);

js::Logger::Colored << "~y~[JS] Freeing environment" << js::Logger::Endl;
node::FreeEnvironment(env);
js::Logger::Colored << "~y~[JS] Freeing isolate data" << js::Logger::Endl;
node::FreeIsolateData(nodeData);

js::Logger::Colored << "~y~[JS] Closing uv loop" << js::Logger::Endl;
uv_loop_close(uvLoop);
delete uvLoop;

js::Logger::Colored << "~y~[JS] Resetting resource" << js::Logger::Endl;
IResource::Reset();
js::Logger::Colored << "~y~[JS] Cleaning up metrics" << js::Logger::Endl;
IMetricHandler::CleanupMetrics();

js::Logger::Colored << "~y~[JS] Resource stopped" << js::Logger::Endl;
return true;
}

Expand Down
9 changes: 7 additions & 2 deletions shared/src/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ void js::Event::SendEvent(const alt::CEvent* ev, IResource* resource)

js::Promise promise = CallEventBinding(false, (int)ev->GetType(), eventArgs, resource);
eventArgs.Get()->SetAlignedPointerInInternalField(1, nullptr);
if(!promise.IsValid()) return;
if(ev->GetType() == alt::CEvent::Type::RESOURCE_STOP && static_cast<const alt::CResourceStopEvent*>(ev)->GetResource() == resource->GetResource()) promise.Await();

if (!promise.IsValid()) return;

if (ev->GetType() == alt::CEvent::Type::RESOURCE_STOP && static_cast<const alt::CResourceStopEvent*>(ev)->GetResource() == resource->GetResource())
{
promise.Await();
}
}

void js::Event::SendEvent(EventType type, EventArgs& args, IResource* resource)
Expand Down
6 changes: 6 additions & 0 deletions shared/src/helpers/JS.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,18 @@ namespace js
while(true)
{
v8::Promise::PromiseState state = promise->State();

switch(state)
{
case v8::Promise::PromiseState::kPending: internal::RunEventLoop(); break;
case v8::Promise::PromiseState::kFulfilled: return true;
case v8::Promise::PromiseState::kRejected: return false;

// NOTE (xLuxy): I have no idea why state can be 3 or what it means - it's undocumented
// state is probably state - 1?
case 3: return false;
}

std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
Expand Down

0 comments on commit 1370065

Please sign in to comment.