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 off by 1 issue in when relying on keySystems[].maxSessionCacheSize #1565

Merged
merged 2 commits into from
Oct 15, 2024

Conversation

peaBerberian
Copy link
Collaborator

@peaBerberian peaBerberian commented Oct 4, 2024

keySystems[].maxSessionCacheSize, which allows to limit the number of created MediaKeySession, was relied on before creating a new MediaKeySession (this is good I guess).

However it considered if we were over the limit at that instant, were in reality the logic should probably also account for the MediaKeySession that will be created just after.

The current behavior leads to having a supplementary MediaKeySession than what keySystems[].maxSessionCacheSize authorizes.

To fix it, I simply remove 1 from the limit we want to enforce in that call, unless removing 1 would lead to a negative number as this has a special semantic (which is to have no limit). This still works for 0 like it worked before (which is strangely equivalent to setting it to 1 in our logic, but setting it to 0 is just asking for bugs anyway I guess :p).

NOTE: We could have a plausible justification for our precedent behavior that it was only setting the "cache size" and thus excluding the last "actually relied on" one, but this is not how it is documented in the API documentation, so to me this is a bug.

@peaBerberian peaBerberian added bug This is an RxPlayer issue (unexpected result when comparing to the API) DRM Relative to DRM (EncryptedMediaExtensions) Priority: 1 (High) This issue or PR has a high priority. labels Oct 4, 2024
@peaBerberian peaBerberian added this to the 4.2.0 milestone Oct 4, 2024
`keySystems[].maxSessionCacheSize', which allows to limit the number of
created `MediaKeySession`, was relied on before creating a new `MediaKeySession`
(this is good I guess).

However it considered if we were over the limit at that instant, were in
reality the logic should probably also account for the `MediaKeySession`
that will be created just after.

The current behavior leads to having a supplementary `MediaKeySession`
than what `keySystems[].maxSessionCacheSize` authorizes.

To fix it, I simply remove `1` from the limit we want to enforce in that
call, unless removing `1` would lead to a negative number as this has a
special semantic (which is to have no limit). This still works for `0`
like it worked before (which is strangely equivalent to setting it to
`1` in our logic, but setting it to `0` is just asking for bugs anyway
I guess :p).

NOTE: We could have a plausible justification for our precedent behavior
that it was only setting the "cache size" and thus excluding the last
"actually relied on" one, but this is not how it is documented in the
API documentation, so to me this is a bug.
@peaBerberian peaBerberian force-pushed the fix/maxSessionCacheSize-off-by-one branch from 9953ddb to 608ecbb Compare October 4, 2024 13:54
@@ -30,7 +30,7 @@ export default async function cleanOldLoadedSessions(
loadedSessionsStore: LoadedSessionsStore,
limit: number,
): Promise<void> {
if (limit < 0 || limit >= loadedSessionsStore.getLength()) {
if (limit < 0 || limit > loadedSessionsStore.getLength()) {
Copy link
Collaborator

@Florent-Bouisset Florent-Bouisset Oct 15, 2024

Choose a reason for hiding this comment

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

I'm not sure this is right:
If maxSessionCacheSize was set to 2 in the API, then limit here is equal to maxSessionCacheSize - 1 so limit is 1.

So before creating second session loadedSessionsStore.getLength() is 1.
Then 1 > 1 returns false and we clean the old sessions,
thus we should be able to have 2 sessions!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It seems to me that you're right though we wouldn't be closing anything thanks to:

const toDelete = entries.length - limit;

I'm not sure of what I was thinking here.

Can you fix it on your side? It's hard to develop in my current setup.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I have changed it, I removed the change on this file.
Basically this file was already fine, and behaves like cleanOldStoredPersistentInfo.

The only change needed is create_or_load_session.ts, that have the logic of setting the limit minus 1 because it's about to create a session.

The equivalent is already done for persistent sessions in the content decryptor:

cleanOldStoredPersistentInfo(
  persistentSessionsStore,
  EME_MAX_STORED_PERSISTENT_SESSION_INFORMATION - 1,
);

@peaBerberian peaBerberian merged commit 38e1d93 into dev Oct 15, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This is an RxPlayer issue (unexpected result when comparing to the API) DRM Relative to DRM (EncryptedMediaExtensions) Priority: 1 (High) This issue or PR has a high priority.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants