Repository navigation
Description
This is a Bug Report
Description
-
What went wrong?
Multiple invocations of a single-referenced asynchronously rendered variable. Certain patterns of variable use lead to very unexpected results with timing and other inconsistencies. See details in following example. -
What did you expect should have happened?
Render the asynchronous configuration fetch once. -
What was the config you used?
Reproduction using below here: https://github.com/erikerikson/serverless-test/tree/async-fetch -
What stacktrace or error message from your provider did you see?
N/A
Example...
Input to populateObject:
custom: {
item9: '${self:custom.item7}',
item7: '${self:custom.item5}',
item5: '${self:custom.item3}',
item3: '${self:custom.item1}',
item1: '${self:custom.val}',
item2: '${self:custom.item1}',
item4: '${self:custom.item3}',
item6: '${self:custom.item5}',
item8: '${self:custom.item7}',
val: '${file(async.load.js):}',
},
}
code for async.load.js:
let i = 0
module.exports = () => new Promise((resolve) => {
setTimeout(() => {
i += 1 // side effect
resolve(`my-async-value-${i}`)
}, 200)
})
result:
{
"custom": {
"item9": "my-async-value-9",
"item7": "my-async-value-7",
"item5": "my-async-value-5",
"item3": "my-async-value-3",
"item1": "my-async-value-2",
"item2": "my-async-value-4",
"item4": "my-async-value-6",
"item6": "my-async-value-8",
"item8": "my-async-value-10",
"val": "my-async-value-1"
}
}
expected:
{
"custom": {
"item9": "my-async-value-1",
"item7": "my-async-value-1",
"item5": "my-async-value-1",
"item3": "my-async-value-1",
"item1": "my-async-value-1",
"item2": "my-async-value-1",
"item4": "my-async-value-1",
"item6": "my-async-value-1",
"item8": "my-async-value-1",
"val": "my-async-value-1"
}
}
While this is a trivial and maybe adversarial case, consider the more common and pedestrian case of retrieving values from a database. What happens if the database contents are retrieved multiple times during the resolution of SLS variables and between each retrieval (or even just one of them) the values are changed?
Similar or dependent issues:
- see also PR Added support for circular references in the variable system #4144 (this was discovered in looking for tests cases there)
Additional Data
- Serverless Framework Version you're using: master
- Operating System: osx
- Stack Trace: n/a
- Provider Error messages: n/a
Activity
HyperBrain commentedon Dec 5, 2017
This might already be resolved with the variable cache and optimization (fully available in the
cache-AWS-requestsbranch until merged)erikerikson commentedon Dec 7, 2017
Although #4534 fixes these test cases, it seems not to cover cases similar to:
${file('async.load.js'):foo}${file('async.load.js'):bar}Since the variable strings will be different.
To cover such a case, the variable system would have to differentiate between the loaded thing and the reference into that thing. For the above case, foo and bar could be strings but they could also be a string and a function returning a promise in which case, refetching could be appropriate. Worms in a can, there be.
For each variable type, the meaning of a different variable string could have different implications. For SSM, for example, indexing into parameter should not influence caching but as discussed, doing so with a javascript file should contingently influence caching.
HyperBrain commentedon Dec 7, 2017
As the interpretation of cache keys is dependent on the target technology used (file, SSM, etc.) it could be more appropriate to let the
getValueFromXXXXmethods return an additional flag, if the pointers should be included into the cache key or not. This would make the system extensible for new, yet unknown, technologies where the behavior is not yet known.From an architectural perspective, it could even make sense to abstract each technology including its resolver into classes that offer (1) an
getValueFrom(), (2) the matching regex and (3) the pointer interpretation flag, so that there is e.g. an SSMResolver, that provides the flag that it wants to include the pointer, and an FileResolver, that tells you that it does not want to include them.erikerikson commentedon Dec 12, 2017
Resolved by #4144 and #4534.
Should we open up a more deeply/extensively ranging issue under which to provide per source caching as we've discussed?
HyperBrain commentedon Dec 12, 2017
@erikerikson That would be helpful and allow us to track the cache key improvements separately. Please open a new task for that.