aws / jsii

jsii allows code in any language to naturally interact with JavaScript classes. It is the technology that enables the AWS Cloud Development Kit to deliver polyglot libraries from a single codebase!
https://aws.github.io/jsii
Apache License 2.0
2.66k stars 247 forks source link

Make Jsii Thread Safe #4311

Open fahhas-aws opened 1 year ago

fahhas-aws commented 1 year ago

Describe the feature

Hello,

The Jsii Library is not thread safe. This has been confirmed several times using the CDK library in java. When attempting to build CDK templates in more than one thread, you are greeted with this error.

Exception in thread "Thread-1" software.amazon.jsii.JsiiException: Malformed object reference. Expecting $jsii.byref at software.amazon.jsii.JsiiObjectRef.parse(JsiiObjectRef.java:52) at software.amazon.jsii.JsiiClient.createObject(JsiiClient.java:91) at software.amazon.jsii.JsiiEngine.createNewObject(JsiiEngine.java:458)

The current workaround is to synchronize requests via a lock in Java when performing any actions with CDK.

Please update jsii to be thread safe.

Use Case

Attempting to build CloudFormation templates in more than one thread by leveraging CDK with Java.

Proposed Solution

There seems to be a static singleton of the JsiiEngine instance that is used to build constructs in CDK - regardless of what app is using it. Instead of JsiiEngine.getInstance() this could instead be new JsiiEngine(..) that could be used by an app object upon its creation.

Would investigate in updating all statically called methods that reference any jsii related objects to instead be standard object -> method calls instead.

Other Information

No response

Acknowledgements

CDK version used

v2.103.0

Environment details (OS name and version, etc.)

Mac OS Ventura

sthomson-wyn commented 5 days ago

Duplicate of https://github.com/aws/jsii/issues/685