Is checked exception behaviour still appropriate in view of Kotlin? - spring-transactions

The default behaviour of #Transactional methods that throw exceptions is quite widely discussed (eg see search results here) and clearly documented in the Javadoc of org.springframework.transaction.interceptor.DefaultTransactionAttribute. Essentially, runtime exceptions and errors cause a rollback, but checked exceptions do not.
However, with increased use of Kotlin I think there is increased danger of fairly catastrophic problems creeping into code unawares, and I'm wondering if there should be any consideration of changing the behaviour of DefaultTransactionAttribute to be in line with the behaviour of TransactionTemplate, which does rollback on checked exceptions.
The problem I uncovered was in a piece of code where a #Transactional Kotlin method was indirectly calling a Java method which declared a checked exception - an Exception which would actually occur very very rarely.
The chain of Kotlin method calls leading to the Java method did not catch and deal with the checked exception at any point, and of course the compiler is perfectly happy with that. Inarguably the writer of the code or a reviewer should have spotted the omission, but realistically, it is the sort of problem that can quite easily be missed. If it is missed the outcome is really quite bad - in my case the transaction was committed even though only half the expected entities had been created at the point everything blew up, and the actual exception was swallowed in TransactionAspectSupport so wasn't visible anywhere. I only discovered the problem because (luckily) there was an integrity constraint violation in a subsequent piece of code.
Is this a case where the impact is high enough that the framework code should protect developers from themselves? In Java it is very difficult to accidentally make this mistake, but in Kotlin it is a different matter.

Related

GroovyRuntimeException: Ambiguous method overloading

I am working on a large code base that was written 90% in Java and 10% in Groovy. I only started learning Groovy recently and would like some advice. I have come across code like this in a Groovy method:
throw new RuntimeException(getErrorMessage(state));
This works most of the time. The problem is that on this very rare occasion, getErrorMessage(state) returned null instead of a String. I cannot change the implementation of getErrorMessage().
I can fix this particular issue like this:
throw new RuntimeException((String) getErrorMessage(state));
or like this:
throw new RuntimeException(getErrorMessage(state) ?: '(no detail available)');
This issue would not arise if I used #CompileStatic, but there are other parts of the code that rely on dynamic features.
My problem is that I can only fix such issues by coming across them as a result of a bug report, which is not good for the application's reliability and reputation. (Also the problem is not just with getErrorMessage but with all possible situations in which an ambiguous overload exception can happen at runtime.)
How can I foresee such issues beforehand? (Currently available static analysis tools don't seem to be clever enough to catch this.)
Do a search for all getErrorMessage() and replace them for myGetErrorMessage():
public String myGetErrorMessage(int state) {
return getErrorMessage(state) ?: '(no detail available)';
}
Aspect-Oriented Programming may be overkill for this particular case, but it will get you what you want. You will still need to somehow identify all the methods that need intervention. This is an article describing several AOP libraries.

Is there any classification of standard Python exceptions to transient and permanent errors?

In my current project I have modules communicating using simple request/reply form of RPC (remote procedure calls). I want to automatically retry failed requests if and only if there is a chance that a new attempt might be successfull.
Vast majority of errors are permanent, but errors like timeouts and I/O errors are not.
I have defined two custom exceptions - RPCTransientError and RPCPermanentError - and currently I map all errors to one of these two exceptions. If in doubt, I choose the transient one.
I do not want to reinvent the wheel. My question is: is there any existing resource regarding classification of standard exceptions to transient and permanent errors?
I'm using Python 3.3+ with the new OS and IO related exception hierarchy that I like a lot. (PEP 3151). Don't care about previous versions.
No, there is not for the simple reason an error may be transit for you while for someone else it may be seen as permanent, depending on the usage.
If in doubt, I choose the transient one.
If even you can't be sure about the errors from the software you designed, how can someone make this choice to be universal?
One approach is to deal with only the most basic subclasses. You say you want to retry on IO Errors and timeouts; Since Python 3.3, IOError and some other exceptions like socket.error have been merged onto OSError. So, you can simply check for OSError and it will apply for those and many other subclasses like TimeoutError, ConnectionError, FileNotFoundError...
You can see the affected classes with OSError.__subclasses__(). Try on a python shell (do this recursively to find all of them).

Xlib: sequence lost in reply type 0x2

i had an occurrence of this error (Xlib: sequence lost in reply type 0x2) in a program i'm maintaining (i'm not the original developer).
I'm far from being an expert in Xlib programming, and i included motif in the tag only because this program was written using that toolkit.
I did some research before posting, and found out that this error is probably due to a thread (different from the UI's one) which is trying to update the UI itself. Searching in the code i found some calls to XTestFakeKeyEvent and XtIsManaged which i'm sure are used in a different thread from the UI.
My question is: could these two functions originate this error?
I would think that only functions that do update the GUI (e.g. set the text of a label) could cause problems of that sort (and these 2 functions don't seem to directly impact the gui), but i honestly don't know....
XTestFakeKeyEvent is most likely the culprit. It injects a key press/release event, which could mess up the event queue.
XtIsManaged will not cause a change, but may give the wrong result if the managed state is changed during it's execution.

When exactly am I required to set objects to nothing in classic asp?

On one hand the advice to always close objects is so common that I would feel foolish to ignore it (e.g. VBScript Out Of Memory Error).
However it would be equally foolish to ignore the wisdom of Eric Lippert, who appears to disagree: http://blogs.msdn.com/b/ericlippert/archive/2004/04/28/when-are-you-required-to-set-objects-to-nothing.aspx
I've worked to fix a number of web apps with OOM errors in classic asp. My first (time consuming) task is always to search the code for unclosed objects, and objects not set to nothing.
But I've never been 100% convinced that this has helped. (That said, I have found it hard to pinpoint exactly what DOES help...)
This post by Eric is talking about standalone VBScript files, not classic ASP written in VBScript. See the comments, then Eric's own comment:
Re: ASP -- excellent point, and one that I had not considered. In ASP it is sometimes very difficult to know where you are and what scope you're in.
So from this I can say that everything he wrote isn't relevant for classic ASP i.e. you should always Set everything to Nothing.
As for memory issues, I think that assigning objects (or arrays) to global scope like Session or Application is the main reason for such problems. That's the first thing I would look for and rewrite to hold only single identifider in Session then use database to manage the data.
Basically by setting a COM object to Nothing, you are forcing its terminator to run deterministically, which gives you the opportunity to handle any errors it may raise.
If you don't do it, you can get into a situation like the following:
Your code raises an error
The error isn't handled in your code and therefore ...
other objects instantiated in your code go out of scope, and their terminators run
one of the terminators raises an error
and the error that is propagated is the one from the terminator going out of scope, masking the original error.
I do remember from the dark and distant past that it was specifically recommended to close ADO objects. I'm not sure if this was because of a bug in ADO objects, or simply for the above reason (which applies more generally to any objects that can raise errors in their terminators).
And this recommendation is often repeated, though often without any credible reason. ("While ASP should automatically close and free up all object instantiations, it is always a good idea to explicitly close and free up object references yourself").
It's worth noting that in the article, he's not saying you should never worry about setting objects to nothing - just that it should not be the default behaviour for every object in every script.
Though I do suspect he's a little too quick to dismiss the "I saw this elsewhere" method of coding behaviour, I'm willing to bet that there is a reason Eric didn't consider that has caused this to be passed along as a hard 'n' fast rule - dealing with junior programmers.
When you start looking more closely at the Dreyfus model of skill acquisition, you see that at the beginning levels of acquiring a new skill, learners need simple to follow recipes. They do not yet have the knowledge or ability to make the judgement calls Eric qualifies the recommendation with later on.
Think back to when you first started programming. Could you readily judge if you were "set[tting an] expensive objects to Nothing when you are done with them if you are done with them well before they go out of scope"? Did you really know which objects were expensive or when they truly went out of scope?
Thus, most entry level programmers are simply told "always set every object to Nothing when you are done with it" because it is within their grasp to understand and follow. Unfortunately, not many programmers take the time to self-educate, learn, and grow into the higher-level Dreyfus stages where you can use the more nuanced situational approach.
And then we come back to my earlier statement - even the best of us started out at that earlier stage, where we reflexively closed all objects because that was the best we were capable of. We left large bodies of code that people look at now, and project our current competence backwards to the earlier work and assume we did that for reasons we don't understand.
I've got to get going, but I hope to expand this a little futher...

How to detect and debug multi-threading problems?

This is a follow up to this question, where I didn't get any input on this point. Here is the brief question:
Is it possible to detect and debug problems coming from multi-threaded code?
Often we have to tell our customers: "We can't reproduce the problem here, so we can't fix it. Please tell us the steps to reproduce the problem, then we'll fix it." It's a somehow nasty answer if I know that it is a multi-threading problem, but mostly I don't. How do I get to know that a problem is a multi-threading issue and how to debug it?
I'd like to know if there are any special logging frameworks, or debugging techniques, or code inspectors, or anything else to help solving such issues. General approaches are welcome. If any answer should be language related then keep it to .NET and Java.
Threading/concurrency problems are notoriously difficult to replicate - which is one of the reasons why you should design to avoid or at least minimize the probabilities. This is the reason immutable objects are so valuable. Try to isolate mutable objects to a single thread, and then carefully control the exchange of mutable objects between threads. Attempt to program with a design of object hand-over, rather than "shared" objects. For the latter, use fully synchronized control objects (which are easier to reason about), and avoid having a synchronized object utilize other objects which must also be synchronized - that is, try to keep them self contained. Your best defense is a good design.
Deadlocks are the easiest to debug, if you can get a stack trace when deadlocked. Given the trace, most of which do deadlock detection, it's easy to pinpoint the reason and then reason about the code as to why and how to fix it. With deadlocks, it always going to be a problem acquiring the same locks in different orders.
Live locks are harder - being able to observe the system while in the error state is your best bet there.
Race conditions tend to be extremely difficult to replicate, and are even harder to identify from manual code review. With these, the path I usually take, besides extensive testing to replicate, is to reason about the possibilities, and try to log information to prove or disprove theories. If you have direct evidence of state corruption you may be able to reason about the possible causes based on the corruption.
The more complex the system, the harder it is to find concurrency errors, and to reason about it's behavior. Make use of tools like JVisualVM and remote connect profilers - they can be a life saver if you can connect to a system in an error state and inspect the threads and objects.
Also, beware the differences in possible behavior which are dependent on the number of CPU cores, pipelines, bus bandwidth, etc. Changes in hardware can affect your ability to replicate the problem. Some problems will only show on single-core CPU's others only on multi-cores.
One last thing, try to use concurrency objects distributed with the system libraries - e.g in Java java.util.concurrent is your friend. Writing your own concurrency control objects is hard and fraught with danger; leave it to the experts, if you have a choice.
I thought that the answer you got to your other question was pretty good. But I'll emphasis these points.
Only modify shared state in a critical section (Mutual Exclusion)
Acquire locks in a set order and release them in the opposite order.
Use pre-built abstractions whenever possible (Like the stuff in java.util.concurrent)
Also, some analysis tools can detect some potential issues. For example, FindBugs can find some threading issues in Java programs. Such tools can't find all problems (they aren't silver bullets) but they can help.
As vanslly points out in a comment to this answer, studying well placed logging output can also very helpful, but beware of Heisenbugs.
For Java there is a verification tool called javapathfinder which I find it useful to debug and verify multi-threading application against potential race condition and death-lock bugs from the code.
It works finely with both Eclipse and Netbean IDE.
[2019] the github repository
https://github.com/javapathfinder
Assuming I have reports of troubles that are hard to reproduce I always find these by reading code, preferably pair-code-reading, so you can discuss threading semantics/locking needs. When we do this based on a reported problem, I find we always nail one or more problems fairly quickly. I think it's also a fairly cheap technique to solve hard problems.
Sorry for not being able to tell you to press ctrl+shift+f13, but I don't think there's anything like that available. But just thinking about what the reported issue actually is usually gives a fairly strong sense of direction in the code, so you don't have to start at main().
In addition to the other good answers you already got: Always test on a machine with at least as many processors / processor cores as the customer uses, or as there are active threads in your program. Otherwise some multithreading bugs may be hard to impossible to reproduce.
Apart from crash dumps, a technique is extensive run-time logging: where each thread logs what it's doing.
The first question when an error is reported, then, might be, "Where's the log file?"
Sometimes you can see the problem in the log file: "This thread is detecting an illegal/unexpected state here ... and look, this other thread was doing that, just before and/or just afterwards this."
If the log file doesn't say what's happening, then apologise to the customer, add sufficiently-many extra logging statements to the code, give the new code to the customer, and say that you'll fix it after it happens one more time.
Sometimes, multithreaded solutions cannot be avoided. If there is a bug,it needs to be investigated in real time, which is nearly impossible with most tools like Visual Studio. The only practical solution is to write traces, although the tracing itself should:
not add any delay
not use any locking
be multithreading safe
trace what happened in the correct sequence.
This sounds like an impossible task, but it can be easily achieved by writing the trace into memory. In C#, it would look something like this:
public const int MaxMessages = 0x100;
string[] messages = new string[MaxMessages];
int messagesIndex = -1;
public void Trace(string message) {
int thisIndex = Interlocked.Increment(ref messagesIndex);
messages[thisIndex] = message;
}
The method Trace() is multithreading safe, non blocking and can be called from any thread. On my PC, it takes about 2 microseconds to execute, which should be fast enough.
Add Trace() instructions wherever you think something might go wrong, let the program run, wait until the error happens, stop the trace and then investigate the trace for any errors.
A more detailed description for this approach which also collects thread and timing information, recycles the buffer and outputs the trace nicely you can find at:
CodeProject: Debugging multithreaded code in real time 1
A little chart with some debugging techniques to take in mind in debugging multithreaded code.
The chart is growing, please leave comments and tips to be added.
(update file at this link)
Visual Studio allows you to inspect the call stack of each thread, and you can switch between them. It is by no means enough to track all kinds of threading issues, but it is a start. A lot of improvements for multi-threaded debugging is planned for the upcoming VS2010.
I have used WinDbg + SoS for threading issues in .NET code. You can inspect locks (sync blokcs), thread call stacks etc.
Tess Ferrandez's blog has good examples of using WinDbg to debug deadlocks in .NET.
assert() is your friend for detecting race-conditions. Whenever you enter a critical section, assert that the invariant associated with it is true (that's what CS's are for). Though, unfortunately, the check might be expensive and thus not suitable for use in production environment.
I implemented the tool vmlens to detect race conditions in java programs during runtime. It implements an algorithm called eraser.
Develop code the way that Princess recommended for your other question (Immutable objects, and Erlang-style message passing). It will be easier to detect multi-threading problems, because the interactions between threads will be well defined.
I faced a thread issue which was giving SAME wrong result and was not behaving un-predictably since each time other conditions(memory, scheduler, processing load) were more or less same.
From my experience, I can say that HARDEST PART is to recognize that it is a thread issue, and BEST SOLUTION is to review the multi-threaded code carefully. Just by looking carefully at the thread code you should try to figure out what can go wrong. Other ways (thread dump, profiler etc) will come second to it.
Narrow down on the functions that are being called, and rule out what could and could not be to blame. When you find sections of code that you suspect may be causing the issue, add lots of detailed logging / tracing to it. Once the issue occurs again, inspect the logs to see how the code executed differently than it does in "baseline" situations.
If you are using Visual Studio, you can also set breakpoints and use the Parallel Stacks window. Parallel Stacks is a huge help when debugging concurrent code, and will give you the ability to switch between threads to debug them independently. More info-
https://learn.microsoft.com/en-us/visualstudio/debugger/using-the-parallel-stacks-window?view=vs-2019
https://learn.microsoft.com/en-us/visualstudio/debugger/walkthrough-debugging-a-parallel-application?view=vs-2019
I'm using GNU and use simple script
$ more gdb_tracer
b func.cpp:2871
r
#c
while (1)
next
#step
end
The best thing I can think of is to stay away from multi-threaded code whenever possible. It seems there are very few programmers who can write bug free multi threaded applications and I would argue that there are no coders beeing able to write bug free large multi threaded applications.

Resources