aws / aws/jsii

Make Jsii Thread Safe

Open
#4,311 1 comment 0 reactions 0 assignees View on GitHub
feature-request p2
Dominant language
TypeScript
Stars
2.9k
Forks
267
Avg merge
1d 25m
Merged PRs (30d)
14

Description

### 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

- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

v2.103.0

### Environment details (OS name and version, etc.)

Mac OS Ventura

Contributor guide

Open the contributing guide

Research direction

Start by tracing the static JsiiEngine.getInstance() usage and the Java paths named in JsiiObjectRef.java, JsiiClient.java, and JsiiEngine.java. Reproduce concurrent CDK template construction with Java, then map the shared state involved. Done means independent concurrent requests no longer produce malformed object references, with tests covering the thread-safe behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.