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 non-thread safe code related to text date times #197

Conversation

stevedlawrence
Copy link
Member

We create an ICU Calendar object to handle parsing/unparsing text date
times. Unfortunately, this Calendar object isn't thread safe, but we
share it among all threads when all the DFDL calendar properties are
constant. This can lead to random errors.

To make it thread safe, change the text calendar code to never modify
the original Calendar object, but instead create a clone of it before
use and use the clone for all dateTime calculations. The effectively
treats the original calendar as immutable and avoids thread safety
issues. Also reorganize the code a bit and rename some variables to make
the purpose of the different Calendar objects more clear.

DAFFODIL-2097

Copy link
Contributor

@mbeckerle mbeckerle left a comment

Choose a reason for hiding this comment

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

+1

// so the below code could modify the same object at the same time
// across threads. By cloning, we ensure that they modify different
// objects.
val calendar = calendarOrig.clone().asInstanceOf[Calendar]
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess I agree, we should just allocate a new calendar here. If these things are going to be so stateful no point in bending over backwards to cache/avoid allocating them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's pretty unfortunate. We could alternatively do thread local stuff, but it just becomes a pain and is more complex. And I'm not sure if there's a big gain, I would imagine the synchronizing that a thread local requires is more overhead that allocating a new Calendar object--it's not a super complex object.

Ultimately, we should switch to library that isn't so stateful.

Copy link
Contributor

@jadams-tresys jadams-tresys left a comment

Choose a reason for hiding this comment

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

+1

We create an ICU Calendar object to handle parsing/unparsing text date
times. Unfortunately, this Calendar object isn't thread safe, but we
share it among all threads when all the DFDL calendar properties are
constant. This can lead to random errors.

To make it thread safe, change the text calendar code to never modify
the original Calendar object, but instead create a clone of it before
use and use the clone for all dateTime calculations. The effectively
treats the original calendar as immutable and avoids thread safety
issues. Also reorganize the code a bit and rename some variables to make
the purpose of the different Calendar objects more clear.

DAFFODIL-2097
@stevedlawrence stevedlawrence force-pushed the daffodil-2097-calendar-thread-safe branch from 7dfbc2c to 1361480 Compare March 15, 2019 17:42
@stevedlawrence stevedlawrence merged commit a01cc32 into apache:master Mar 15, 2019
@stevedlawrence stevedlawrence deleted the daffodil-2097-calendar-thread-safe branch March 15, 2019 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants