function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Darryl Moyers 20Darryl Moyers 20 

Code behaving differently (correctly) ONLY when Debug Logs are active

Hi All,

We've setup a reasonably straightforward Apex Class that's running via a Lightning Component. Happy to share the code, eventually, but we seem to have narrowed down the specific scope of our problem... and we're stumped.

When we run our code and attach a debug/trace... It works correctly. All logic performs desirably.

When we run our code without a debug/trace... It performs incorrectly. We're not hitting errors, but our hypothesis is that collections that should have had, say, 3 items in them (when the code works properly, and/or when we attach a debug log/trace) may only contain 1 when we don't do the debug.

Does the problem we're describing above sound familiar to anyone? Our suspicion is that the debug logs / traces are either slowing down the code execution (in a way that's desirable for our code...) or holding onto simple collection variables in a way that they normally wouldn't.

Nothing makes sense, but it is repeatable. 

We're dealing with ~100 lines of Apex, 3 DML statements, and all code is properly bulkified. No callouts. Nothing asynchronous. Just vanilla.

When we run it with Debugs, it works fine; when we run it without debugs, it works incorrectly.

Any thoughts?
Best Answer chosen by Darryl Moyers 20
AnudeepAnudeep (Salesforce Developers) 
Hi Darryl,

I see some existing bugs today for example: https://success.salesforce.com/issues_view?id=a1p3A000001RXPlQAO with a workaround

There is a strange phenomenon called Heisenbug. In computer programming, heisenbug is a classification of an unusual software bug that disappears or alters its behavior when an attempt to isolate it. When we try to recreate the bug or use a debugger, the error may change or even vanish.
 
-In most of the cases the issue does not occur when we set the debug level of Apex to FINEST.
-When we reduce the debug level and try to replicate the issue, we will be able to replicate the issue and capture the debug logs.
-Such issue seems to occur when there is an issue with collections (list, set or map) in apex.
 
So, to find the exact error we can try reducing the debug level and capture the debug logs. Once we get the exact error we can work on the fix.
 
One possible workaround until the error is fixed is to add a simple debug statement in the code and the issue never occurs.
  
Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Darryl, 

I have seen issues in the past where there could be overhead caused in terms of CPU time by having debug logs enabled. However, I have never come across something that would not work when debug logs /trace is not captured.

If this is reproducible, have you tried raising a case with Salesforce Support? 

Thanks, 
Anudeep
Darryl Moyers 20Darryl Moyers 20
Hi Anudeep,
Thanks for the reply. Your thoughts echo ours. If anything, we'd expect the functionality to work the opposite of the way we describe.

We looked into logging a Case with Salesforce, but none of the categories make sense. There's not really anything related to development/code, that's what the forums are for.

Which Case category would you suggest?

Thank you,
Darryl
David Zhu 🔥David Zhu 🔥
What if you run as admin without debug turned on? I assume that also failed. Would the log in developer console giving you a hint?
AnudeepAnudeep (Salesforce Developers) 
Hi Darryl,

I see some existing bugs today for example: https://success.salesforce.com/issues_view?id=a1p3A000001RXPlQAO with a workaround

There is a strange phenomenon called Heisenbug. In computer programming, heisenbug is a classification of an unusual software bug that disappears or alters its behavior when an attempt to isolate it. When we try to recreate the bug or use a debugger, the error may change or even vanish.
 
-In most of the cases the issue does not occur when we set the debug level of Apex to FINEST.
-When we reduce the debug level and try to replicate the issue, we will be able to replicate the issue and capture the debug logs.
-Such issue seems to occur when there is an issue with collections (list, set or map) in apex.
 
So, to find the exact error we can try reducing the debug level and capture the debug logs. Once we get the exact error we can work on the fix.
 
One possible workaround until the error is fixed is to add a simple debug statement in the code and the issue never occurs.
  
Anudeep
This was selected as the best answer
Darryl Moyers 20Darryl Moyers 20
Kudos, Anudeep! Thank you for the thoughtful reply.

That was exactly the bug we were hitting. Our code has been remedied and pushed live, just by adding a debug statement before the .contains() method is used on a List.

So, to be clear, the following was not working because of the Known Issue:
if(schedulingEnabledIds.contains(newOLI.PricebookEntryId)){
   integer remainingMonths = Integer.valueOf(opp.Opportunity_Term_Months__c);
   Double annualPrice = newOLI.TotalPrice;
   .....

The commented line below fixed it:
//Use a System.debug() on the List of IDs *BEFORE* using the .contains() method on the list
System.debug(schedulingEnabledIds);  // This addition was a workaround that fixed the problem
if(schedulingEnabledIds.contains(newOLI.PricebookEntryId)){
     integer remainingMonths = Integer.valueOf(opp.Opportunity_Term_Months__c);
     Double annualPrice = newOLI.TotalPrice;

Thank you again,
Darryl
 
adam smith 112adam smith 112
thanx for solution
oneboxhd  (https://oneboxhd.me/) for movie