STS allows to bypass any checked exceptions

forax at univ-mlv.fr forax at univ-mlv.fr
Mon May 15 13:47:48 UTC 2023


----- Original Message -----
> From: "Ron Pressler" <ron.pressler at oracle.com>
> To: "Remi Forax" <forax at univ-mlv.fr>
> Cc: "loom-dev" <loom-dev at openjdk.org>
> Sent: Monday, May 15, 2023 12:42:16 AM
> Subject: Re: STS allows to bypass any checked exceptions

> Both are awful and the second isn’t only awful but clearly uses the API in a way
> the documentation warns against. The purpose of STS is for the scope to not
> handle subtasks individually but always as a unit. Therefore, exception and
> result handling should be done collectively. However, we offer a way to get
> individual subtasks’ results to address the specific but common case where
> different subtasks return different types. The ISE thrown by get means one
> thing: there’s a bug in your code because you’ve used the API incorrectly.
> 
> If you find your scope code interested in a particular task’s exception, you’re
> either doing something advanced or wrong. In fact, if you’re using STS without
> a policy you’re either wrong or advanced.

I agree, for both ShutdownXX policies, accessing to a particular task's exception is a bug.
But the class STS itself is not abstract, and in that case it makes sense to use the task exception, by example, if you have two different pricing API and you want a semantics like this, if both services answer, take the minimum price, or take the price of the one that answer otherwise if none answer, throws the first exception. 

> 
> One decision we wanted to postpone was whether or not to force users to use a
> policy. If real users end up doing things similar to what you’re demonstrating
> — which I guess we'll find out during the preview — that may well be what we
> do. We wanted to keep the option of not using a policy open for the benefit of
> advanced users, and obviously they can use the API incorrectly (in this case —
> intentionally). But if regular users fall into the same trap, we may choose to
> enforce what is now merely a recommendation.
> 
> — Ron

Rémi

> 
>> On 13 May 2023, at 17:33, Remi Forax <forax at univ-mlv.fr> wrote:
>> 
>> The more I think about the way STS/TaskHandle manages checked exceptions the
>> less I like it.
>> 
>> Here is my problem:
>> 
>> Do you agree that this code is kind of awful ?
>> 
>>    static <T> T bypassCheckedException(Callable<? extends T> callable) {
>>        try {
>>            return callable.call();
>>        } catch (Exception e) {
>>            throw new IllegalStateException("task complete with exception");
>>        }
>>    }
>> 
>> so why this other one is not
>> 
>>    static <T> T bypassCheckedException(Callable<? extends T> callable) {
>>        try(var sts = new StructuredTaskScope<>()) {
>>            var task = sts.fork(callable);
>>            try {
>>                sts.join();
>>            } catch (InterruptedException e) {
>>                Thread.currentThread().interrupt();
>>            }
>>            return task.get();
>>        }
>>    }
>> 
>> given that it has exactly the same semantics ?
>> 
>> regards,
> > Rémi


More information about the loom-dev mailing list