Skip to content

Commit 3de0cac

Browse files
committed
fix: allow StackSamplerLoopManager to be started after it is stopped
1 parent b7b9c6d commit 3de0cac

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

profiler/src/ProfilerEngine/Datadog.Profiler.Native/CorProfilerCallback.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,7 @@ void CorProfilerCallback::SetStackSamplerEnabled(bool enabled)
20752075
}
20762076
else
20772077
{
2078-
_pStackSamplerLoopManager->Stop();
2078+
_pStackSamplerLoopManager->Stop2(ServiceBase::State::Init);
20792079
}
20802080
}
20812081

profiler/src/ProfilerEngine/Datadog.Profiler.Native/ServiceBase.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ bool ServiceBase::Start()
4444
if (result)
4545
{
4646
_currentState = State::Started;
47+
Log::Debug("Service '", GetName(), "' started successfully.");
4748
}
4849
else
4950
{
@@ -56,6 +57,16 @@ bool ServiceBase::Start()
5657

5758
bool ServiceBase::Stop()
5859
{
60+
return Stop2(State::Stopped);
61+
}
62+
63+
bool ServiceBase::Stop2(State stoppedState)
64+
{
65+
if (stoppedState != State::Stopped && stoppedState != State::Init)
66+
{
67+
Log::Debug("Invalid final state to stop the service '", GetName(), "'. Expected State::Stopped or State::Init. Current state : ", to_string(_currentState.load()));
68+
return false;
69+
}
5970
auto expected = State::Started;
6071
auto exchange = _currentState.compare_exchange_strong(expected, State::Stopping);
6172

@@ -68,7 +79,8 @@ bool ServiceBase::Stop()
6879
auto result = StopImpl();
6980
if (result)
7081
{
71-
_currentState = State::Stopped;
82+
_currentState = stoppedState;
83+
Log::Debug("Service '", GetName(), "' stopped successfully. Current state : ", to_string(stoppedState));
7284
}
7385
else
7486
{
@@ -79,6 +91,7 @@ bool ServiceBase::Stop()
7991
return result;
8092
}
8193

94+
8295
ServiceBase::State ServiceBase::GetState() const
8396
{
8497
return _currentState;

profiler/src/ProfilerEngine/Datadog.Profiler.Native/ServiceBase.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010
class ServiceBase : public IService
1111
{
1212
public:
13+
enum class State
14+
{
15+
Init,
16+
Starting,
17+
Started,
18+
Stopping,
19+
Stopped
20+
};
1321
ServiceBase();
1422

1523
bool Start() final override;
1624
bool Stop() final override;
1725

26+
// Like Stop, but allows to specify stopped state - either Stopped or Init. Init allows the service to be restarted.
27+
bool Stop2(State stoppedState); // todo better name
28+
1829
protected:
1930
virtual bool StartImpl() = 0;
2031
virtual bool StopImpl() = 0;
2132

22-
enum class State
23-
{
24-
Init,
25-
Starting,
26-
Started,
27-
Stopping,
28-
Stopped
29-
};
33+
3034

3135
State GetState() const;
3236

0 commit comments

Comments
 (0)