Skip to content

fix(service-worker): make ngsw.json generation deterministic and correct - #43679

Closed
gkalpak wants to merge 2 commits into
angular:masterfrom
gkalpak:fix-sw-config-asset-race
Closed

gkalpak wants to merge 2 commits into
angular:masterfrom
gkalpak:fix-sw-config-asset-race

Conversation

@gkalpak

@gkalpak gkalpak commented Oct 2, 2021 •

Copy link
Copy Markdown
Member

Previously, all asset-groups from ngsw-config.json were processed in parallel. For each asset-group, we retrieved all files for the current build, filtered out files that were already matched by other asset-groups, determined which of the remaining files belonged to the current asset-group and generated entries for the ngsw.json manifest. This process was susceptible to race conditions when there were files that would be matched by multiple asset-groups. This made the generation of the ngsw.json manifest non-deterministic and violated the rule that each file would belong to the first asset-group that matched it (based on the asset-groups' order of appearance in ngsw-config.json), thus leading to broken ServiceWorker behavior.

This commit fixes it by ensuring that the generation process is deterministic and that asset-groups are processed in the proper order.

NOTE 1:
The generation process has been broken since the beginning, but we have only noticed this recently. This is possibly related to the CLI's switching from a virtual file system host (which has more consistent timing characteristics) to the Node.js built-in fs.promises in angular/angular-cli@d3bc530.

NOTE 2:
This commit also ensures that files in the ngsw.json hash-table are in alphabetic order. Previously, the files were added to the hash-table in blocks corresponding to each asset-group.
This change is not necessary (i.e. the order of keys in the hash-table makes no difference in behavior), but it makes it easier to scan for a file (for example, for debugging purposes).

NOTE:
Once AIO is updated to a version that includes this fix, remove this work-around.

@gkalpak gkalpak added type: bug/fix action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release area: service-worker Issues related to the @angular/service-worker package labels Oct 2, 2021
@ngbot ngbot Bot modified the milestone: Backlog Oct 2, 2021
@google-cla google-cla Bot added the cla: yes label Oct 2, 2021
@mary-poppins

Copy link
Copy Markdown

@gkalpak
gkalpak force-pushed the fix-sw-config-asset-race branch from 9b34c44 to a98238e Compare October 2, 2021 14:43
@mary-poppins

Copy link
Copy Markdown

@gkalpak
gkalpak marked this pull request as ready for review October 2, 2021 15:27
@gkalpak
gkalpak requested a review from IgorMinar October 2, 2021 15:30
@gkalpak gkalpak added state: WIP action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: review The PR is still awaiting reviews from at least one requested reviewer state: WIP labels Oct 3, 2021
gkalpak added a commit to gkalpak/angular that referenced this pull request Oct 3, 2021
…ministic

As explained in angular#43679, currently the generation of the `ngsw.json` SW
manifest is non-deterministic and often incorrect. Until we can update
to an `@angular/service-worker` version that includes the fix from
PR angular#43679, we temporarily work-around the issue by re-generating the
`ngsw.json` manifest after `ng build` using the `ngsw-config` binary
exposed by `@angular/service-worker`.

NOTE:
This works around the issue, because the [FileSystem][1] class used by
the `ngsw-config` binary happens to be synchronous (unlike the
implementation provided by the Angular CLI), thus avoiding the race
conditions described in angular#43679.

[1]: https://github.com/angular/angular/blob/c721135e370b34c840756bcfb22c8119b4c8c452/packages/service-worker/cli/filesystem.ts#L15

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I feel like we need some kind of test to prove out this fix??

@gkalpak
gkalpak force-pushed the fix-sw-config-asset-race branch from a98238e to 1ece2cc Compare October 5, 2021 11:30
@gkalpak

gkalpak commented Oct 5, 2021

Copy link
Copy Markdown
Member Author

I feel like we need some kind of test to prove out this fix??

Sigh 😞 There used to be a time when my word was enough 😛
Added a test 😃

@gkalpak
gkalpak force-pushed the fix-sw-config-asset-race branch from 1ece2cc to 588a00e Compare October 5, 2021 11:39
@mary-poppins

Copy link
Copy Markdown

@petebacondarwin petebacondarwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the test ❤️ ❤️ ❤️ ❤️

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You don't say 😛 ?

Comment on lines 164 to 166

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So this is the key group in this test, right? Since it matches files that should have already been matched by the previous groups.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes.

dylhunn pushed a commit that referenced this pull request Oct 5, 2021
…ministic (#43687)

As explained in #43679, currently the generation of the `ngsw.json` SW
manifest is non-deterministic and often incorrect. Until we can update
to an `@angular/service-worker` version that includes the fix from
PR #43679, we temporarily work-around the issue by re-generating the
`ngsw.json` manifest after `ng build` using the `ngsw-config` binary
exposed by `@angular/service-worker`.

NOTE:
This works around the issue, because the [FileSystem][1] class used by
the `ngsw-config` binary happens to be synchronous (unlike the
implementation provided by the Angular CLI), thus avoiding the race
conditions described in #43679.

[1]: https://github.com/angular/angular/blob/c721135e370b34c840756bcfb22c8119b4c8c452/packages/service-worker/cli/filesystem.ts#L15

PR Close #43687
@gkalpak
gkalpak removed the request for review from IgorMinar October 5, 2021 17:13
@gkalpak gkalpak removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 5, 2021
@gkalpak
gkalpak force-pushed the fix-sw-config-asset-race branch from 588a00e to be62c33 Compare October 5, 2021 17:13
…rrect

Previously, all asset-groups from `ngsw-config.json` were processed in
parallel. For each asset-group, we retrieved all files for the current
build, filtered out files that were already matched by other
asset-groups, determined which of the remaining files belonged to the
current asset-group and generated entries for the `ngsw.json` manifest.
This process was susceptible to race conditions when there were files
that would be matched by multiple asset-groups. This made the generation
of the `ngsw.json` manifest non-deterministic and violated the rule that
each file would belong to the first asset-group that matched it (based
on the asset-groups' order of appearance in `ngsw-config.json`), thus
leading to broken ServiceWorker behavior.

This commit fixes it by ensuring that the generation process is
deterministic and that asset-groups are processed in the proper order.

NOTE 1:
The generation process has been broken since the beginning, but we have
only noticed this recently. This is possibly related to the CLI's
switching from a virtual file system host (which has more consistent
timing characteristics) to the Node.js built-in `fs.promises` in
angular/angular-cli@d3bc530.

NOTE 2:
This commit also ensures that files in the `ngsw.json` hash-table are in
alphabetic order. Previously, the files were added to the hash-table in
blocks corresponding to each asset-group.
This change is not necessary (i.e. the order of keys in the hash-table
makes no difference in behavior), but it makes it easier to scan for a
file (for example, for debugging purposes).
@mary-poppins

Copy link
Copy Markdown

@gkalpak gkalpak added the action: merge The PR is ready for merge by the caretaker label Oct 5, 2021
@dylhunn

dylhunn commented Oct 5, 2021

Copy link
Copy Markdown
Contributor

This PR was merged into the repository by commit fddb50b.

@dylhunn dylhunn closed this in fddb50b Oct 5, 2021
dylhunn pushed a commit that referenced this pull request Oct 5, 2021
…rrect (#43679)

Previously, all asset-groups from `ngsw-config.json` were processed in
parallel. For each asset-group, we retrieved all files for the current
build, filtered out files that were already matched by other
asset-groups, determined which of the remaining files belonged to the
current asset-group and generated entries for the `ngsw.json` manifest.
This process was susceptible to race conditions when there were files
that would be matched by multiple asset-groups. This made the generation
of the `ngsw.json` manifest non-deterministic and violated the rule that
each file would belong to the first asset-group that matched it (based
on the asset-groups' order of appearance in `ngsw-config.json`), thus
leading to broken ServiceWorker behavior.

This commit fixes it by ensuring that the generation process is
deterministic and that asset-groups are processed in the proper order.

NOTE 1:
The generation process has been broken since the beginning, but we have
only noticed this recently. This is possibly related to the CLI's
switching from a virtual file system host (which has more consistent
timing characteristics) to the Node.js built-in `fs.promises` in
angular/angular-cli@d3bc530.

NOTE 2:
This commit also ensures that files in the `ngsw.json` hash-table are in
alphabetic order. Previously, the files were added to the hash-table in
blocks corresponding to each asset-group.
This change is not necessary (i.e. the order of keys in the hash-table
makes no difference in behavior), but it makes it easier to scan for a
file (for example, for debugging purposes).

PR Close #43679
@gkalpak
gkalpak deleted the fix-sw-config-asset-race branch October 6, 2021 13:10
gkalpak added a commit to gkalpak/angular that referenced this pull request Oct 6, 2021
…ministic

As explained in angular#43679, currently the generation of the `ngsw.json` SW
manifest is non-deterministic and often incorrect. Until we can update
to an `@angular/service-worker` version that includes the fix from
PR angular#43679, we temporarily work-around the issue by re-generating the
`ngsw.json` manifest after `ng build` using the `ngsw-config` binary
exposed by `@angular/service-worker`.

NOTE:
This works around the issue, because the [FileSystem][1] class used by
the `ngsw-config` binary happens to be synchronous (unlike the
implementation provided by the Angular CLI), thus avoiding the race
conditions described in angular#43679.

[1]: https://github.com/angular/angular/blob/c721135e370b34c840756bcfb22c8119b4c8c452/packages/service-worker/cli/filesystem.ts#L15
dylhunn pushed a commit that referenced this pull request Oct 6, 2021
…ministic (#43686)

As explained in #43679, currently the generation of the `ngsw.json` SW
manifest is non-deterministic and often incorrect. Until we can update
to an `@angular/service-worker` version that includes the fix from
PR #43679, we temporarily work-around the issue by re-generating the
`ngsw.json` manifest after `ng build` using the `ngsw-config` binary
exposed by `@angular/service-worker`.

NOTE:
This works around the issue, because the [FileSystem][1] class used by
the `ngsw-config` binary happens to be synchronous (unlike the
implementation provided by the Angular CLI), thus avoiding the race
conditions described in #43679.

[1]: https://github.com/angular/angular/blob/c721135e370b34c840756bcfb22c8119b4c8c452/packages/service-worker/cli/filesystem.ts#L15

PR Close #43686
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Nov 6, 2021
Serginho pushed a commit to TuLotero/angular that referenced this pull request Jan 20, 2022
…rrect (angular#43679)

Previously, all asset-groups from `ngsw-config.json` were processed in
parallel. For each asset-group, we retrieved all files for the current
build, filtered out files that were already matched by other
asset-groups, determined which of the remaining files belonged to the
current asset-group and generated entries for the `ngsw.json` manifest.
This process was susceptible to race conditions when there were files
that would be matched by multiple asset-groups. This made the generation
of the `ngsw.json` manifest non-deterministic and violated the rule that
each file would belong to the first asset-group that matched it (based
on the asset-groups' order of appearance in `ngsw-config.json`), thus
leading to broken ServiceWorker behavior.

This commit fixes it by ensuring that the generation process is
deterministic and that asset-groups are processed in the proper order.

NOTE 1:
The generation process has been broken since the beginning, but we have
only noticed this recently. This is possibly related to the CLI's
switching from a virtual file system host (which has more consistent
timing characteristics) to the Node.js built-in `fs.promises` in
angular/angular-cli@d3bc530.

NOTE 2:
This commit also ensures that files in the `ngsw.json` hash-table are in
alphabetic order. Previously, the files were added to the hash-table in
blocks corresponding to each asset-group.
This change is not necessary (i.e. the order of keys in the hash-table
makes no difference in behavior), but it makes it easier to scan for a
file (for example, for debugging purposes).

PR Close angular#43679
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: service-worker Issues related to the @angular/service-worker package cla: yes target: patch This PR is targeted for the next patch release type: bug/fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants