Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks and other types of leaks #1244

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

marcelotduarte
Copy link
Owner

Fixes #1040
@anthony-tuininga Can you review this for me?

@@ -115,6 +115,7 @@ static int Service_Stop(udt_ServiceInfo* info)
{
PyThreadState *threadState;
PyObject *result;
int ret = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why these changes? If the stop() method could not be called or some other exception takes place, why are you marking the service as stopped anyway?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch I should have put separately. There is another issue that I think may be related.

@@ -164,27 +167,33 @@ static int Service_SessionChange(DWORD sessionId, DWORD eventType)
{
PyThreadState *threadState;
PyObject *result;
char *error_message = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comments apply here. These changes also do not seem to be relevant for the concerns initially raised: namely, memory leaks.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I call this "other types of leaks", because the thread is aquired but not released.

return LogPythonException("unable to import __main__");
if (!module) {
error_message = "unable to import __main__";
goto end;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not at all a fan of goto. If necessary, you can create a separate method (Service_SetupPythonHelper or some such) which will perform the work. There is no real need, though, since if this fails, the entire executable will fail and the process will end.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like 'goto' either. I recently added a new function in util and used 'if succeded' but it's not all good either. As I have been reading some python codes and I see that in some situations they surrender to goto, I surrendered too....


// indicate that the service has stopped
if (Service_SetStatus(info, SERVICE_STOPPED) < 0)
return LogWin32Error(GetLastError(), "cannot set service as stopped");
ret = LogWin32Error(GetLastError(), "cannot set service as stopped");

// now set the control event
if (!SetEvent(gControlEvent))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is now different, since it used to exit early. I'm not familiar with Service_SetStatus or SetEvent so perhaps this is benign?

@@ -468,45 +500,70 @@ static int Service_Install(wchar_t *name, wchar_t *configFileName)
//-----------------------------------------------------------------------------
static int Service_Uninstall(wchar_t *name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that reference counts in Service_Install are not handled, but they are handled in Service_Uninstall. Was Service_Install left out intentionally? In any case, my main concern is the reference counts in Service_SetupPython and Service_Run are addressed (which they are), since any memory leak as part of install or uninstall will be very short lived.

Copy link

codecov bot commented Dec 26, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ecd7210) 51.86% compared to head (b6244de) 51.86%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1244   +/-   ##
=======================================
  Coverage   51.86%   51.86%           
=======================================
  Files          79       79           
  Lines        6177     6177           
=======================================
  Hits         3204     3204           
  Misses       2973     2973           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible (tiny) Memory Leak in Win32Service.c
3 participants