gkralik / php7-sapnwrfc

SAP NW RFC SDK extension for PHP 7.3+ & PHP 8
https://gkralik.github.io/php7-sapnwrfc
MIT License
90 stars 35 forks source link

Memory Leak Issue in SAP PHP Connection: Seeking Advice on Workaround #155

Closed luckkas closed 5 months ago

luckkas commented 11 months ago

Overview

I am currently experiencing a memory leak issue in my SAP PHP connection when using the SAP functions "L_TO_CREATE_SINGLE" and "BAPI_GOODSMVT_CREATE." I have identified a workaround by adjusting the use_function_desc_cache option, and I am seeking advice from the community to better understand the root cause and explore potential improvements.

Issue Details

try { $this->con = new SAPNWRFC\Connection($parameters, $options); return $this->con; } catch (Exception $e) { // Handle exceptions }


## Related Issues
- This issue is related to #119 
- My workaround is related to #136

## Request for Assistance
I am seeking advice on why this workaround solves the issue and whether there are better approaches to address memory leaks in SAP PHP connections. Any insights or suggestions from the community would be greatly appreciated.
pieewiee commented 8 months ago

This problem was driving me nuts. Thank you!!!

gkralik commented 6 months ago

The use_function_desc_cache is the way to go for now. I plan to have another look at this to see where it comes from.

dbielke1986 commented 6 months ago

We have the same issue here. We need to set 'use_function_desc_cache' => false; otherwise we have a fatal error. I will follow this issue.

gkralik commented 6 months ago

I am investigating this, although I haven't been able to reproduce yet.

gkralik commented 6 months ago

Some questions:

Also, @pieewiee & @dbielke1986 can you share your environment (SDK version, extension version, OS, 32/64bit).

And if anyone can test that with the latest SDK version and the latest code of the extension, that would help a lot (function desc cache related change post 2.0.0 - probably unrelated, but who knows)...

dbielke1986 commented 6 months ago

@gkralik

I've just tried it again and I'm unsure if I'm also running into the memory leak, but maybe that's the cause here too, so I'll briefly describe my problem, which meant we had to turn off the “use_function_desc_cache”.

We have the problem that we have to open two connections to an SAP system (client 100 and 200). However, it is not necessarily due to the second connection, but to every second call that we make to SAP to call an RFC. Apparently, it first asks for the descriptions of the fields and then wants to read them from the cache on the second call.

As already described, we get this message on every second call:

SAPNWRFC\FunctionCallException Object
(
    [message:protected] => Failed to get description for parameter T_KUNNR
    [string:Exception:private] => 
    [code:protected] => 20
    [file:protected] => /var/www/html/...php
    [line:protected] => 495
    [trace:Exception:private] => Array

....

    [previous:Exception:private] => 
    [errorInfo:protected] => Array
        (
            [code] => 20
            [key] => RFC_INVALID_PARAMETER
            [message] => field 'T_KUNNR' not found
        )

)

So there seems to be a problem here if any description fields have not been found/maintained, although I have looked everywhere and in my opinion everything is correct.

If this is not in the same direction as the reporter's problem, then please ignore my statement or refer me to a separate item.

Environment:

gkralik commented 6 months ago

@dbielke1986 OK, this sounds a little different.

If the parameters of an RFC change while your script is running, you have to either disable the function description cache completely (use_function_desc_cache) or clear the cache manually.

If you get an error every second call, try the latest version of the extension in the repository (especially including e649a80efaa1fd92e9f30f83a27ecd14795417c1). This fixes a problem with repeated calls of the same RFC, especially when the RemoteFunction object is freed (unset, variable reused).

gkralik commented 6 months ago

Also, can you reproduce the issue with a generic RFC call (like RFC_PING, STFC_STRUCTURE) and if so, can you share a code snippet? This way I can try to debug this.

dbielke1986 commented 6 months ago

@gkralik

I will try to test this with e649a80 and give some feedback.

Thanks for your help and this absolutely incredibly helpful module (essential for us and our use case)!

gkralik commented 6 months ago

I have drafted a new release 2.1.0, if that helps with testing.

Edit: make sure to use the latest SAP SDK (patch level 13 at the time of writing). SAP has been fixing quite a lot of issues, including memory leaks.

dbielke1986 commented 6 months ago

@gkralik

We have updated our page to 2.1.0 + SAP SDK 750 PL13 and everything seems to work right now. Even with use_function_desc_cache = true .

I will monitor this issue for a few more days and then give feedback again. So far, however, everything looks great. Many, many thanks for the module and fixing the problem!

gkralik commented 6 months ago

Good to hear. I suspect it indeed was e649a80...

Keep me posted if anything turns up - I'll leave this open until you report back.

gkralik commented 6 months ago

Nonetheless, I still think the original issue @luckkas faced is something different.

gkralik commented 5 months ago

@dbielke1986 has the issue emerged again or is everything working for you now?

dbielke1986 commented 5 months ago

@gkralik:

Everything is working great! The mentioned fix solves our issue with daily use and hundreds of calls a day. Thank you so much for you fast reply and the quick fix you provided!