summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/docs/AsyncShutdown.rst
blob: 78e3e6c4585ac08d49eb5838e4d96d4f31d335ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
.. _AsyncShutdown:

==============
AsyncShutdown
==============

During shutdown of the process, subsystems are closed one after another. ``AsyncShutdown`` is a module dedicated to express shutdown-time dependencies between:

- services and their clients;
- shutdown phases (e.g. profile-before-change) and their clients.

.. _AsyncShutdown_Barriers:

Barriers: Expressing shutdown dependencies towards a service
============================================================

Consider a service FooService. At some point during the shutdown of the process, this service needs to:

- inform its clients that it is about to shut down;
- wait until the clients have completed their final operations based on FooService (often asynchronously);
- only then shut itself down.

This may be expressed as an instance of ``AsyncShutdown.Barrier``. An instance of ``AsyncShutdown.Barrier`` provides:

- a capability ``client`` that may be published to clients, to let them register or unregister blockers;
- methods for the owner of the barrier to let it consult the state of blockers and wait until all client-registered blockers have been resolved.

Shutdown timeouts
-----------------

By design, an instance of ``AsyncShutdown.Barrier`` will cause a crash
if it takes more than 60 seconds `awake` for its clients to lift or
remove their blockers (`awake` meaning that seconds during which the
computer is asleep or too busy to do anything are not counted). This
mechanism helps ensure that we do not leave the process in a state in
which it can neither proceed with shutdown nor be relaunched.

If the CrashReporter is enabled, this crash will report:

- the name of the barrier that failed;
- for each blocker that has not been released yet:

  - the name of the blocker;
  - the state of the blocker, if a state function has been provided (see :ref:`AsyncShutdown.Barrier.state`).

Example 1: Simple Barrier client
--------------------------------

The following snippet presents an example of a client of FooService that has a shutdown dependency upon FooService. In this case, the client wishes to ensure that FooService is not shutdown before some state has been reached. An example is clients that need write data asynchronously and need to ensure that they have fully written their state to disk before shutdown, even if due to some user manipulation shutdown takes place immediately.

.. code-block:: javascript

    // Some client of FooService called FooClient

    Components.utils.import("resource://gre/modules/FooService.jsm", this);

    // FooService.shutdown is the `client` capability of a `Barrier`.
    // See example 2 for the definition of `FooService.shutdown`
    FooService.shutdown.addBlocker(
      "FooClient: Need to make sure that we have reached some state",
      () => promiseReachedSomeState
    );
    // promiseReachedSomeState should be an instance of Promise resolved once
    // we have reached the expected state

Example 2: Simple Barrier owner
-------------------------------

The following snippet presents an example of a service FooService that
wishes to ensure that all clients have had a chance to complete any
outstanding operations before FooService shuts down.

.. code-block:: javascript

    // Module FooService

    Components.utils.import("resource://gre/modules/AsyncShutdown.jsm", this);
    Components.utils.import("resource://gre/modules/Task.jsm", this);

    this.exports = ["FooService"];

    let shutdown = new AsyncShutdown.Barrier("FooService: Waiting for clients before shutting down");

    // Export the `client` capability, to let clients register shutdown blockers
    FooService.shutdown = shutdown.client;

    // This Task should be triggered at some point during shutdown, generally
    // as a client to another Barrier or Phase. Triggering this Task is not covered
    // in this snippet.
    let onshutdown = Task.async(function*() {
      // Wait for all registered clients to have lifted the barrier
      yield shutdown.wait();

      // Now deactivate FooService itself.
      // ...
    });

Frequently, a service that owns a ``AsyncShutdown.Barrier`` is itself a client of another Barrier.

.. _AsyncShutdown.Barrier.state:

Example 3: More sophisticated Barrier client
--------------------------------------------

The following snippet presents FooClient2, a more sophisticated client of FooService that needs to perform a number of operations during shutdown but before the shutdown of FooService. Also, given that this client is more sophisticated, we provide a function returning the state of FooClient2 during shutdown. If for some reason FooClient2's blocker is never lifted, this state can be reported as part of a crash report.

.. code-block:: javascript

    // Some client of FooService called FooClient2

    Components.utils.import("resource://gre/modules/FooService.jsm", this);

    FooService.shutdown.addBlocker(
      "FooClient2: Collecting data, writing it to disk and shutting down",
      () => Blocker.wait(),
      () => Blocker.state
    );

    let Blocker = {
      // This field contains information on the status of the blocker.
      // It can be any JSON serializable object.
      state: "Not started",

      wait: Task.async(function*() {
        // This method is called once FooService starts informing its clients that
        // FooService wishes to shut down.

        // Update the state as we go. If the Barrier is used in conjunction with
        // a Phase, this state will be reported as part of a crash report if FooClient fails
        // to shutdown properly.
        this.state = "Starting";

        let data = yield collectSomeData();
        this.state = "Data collection complete";

        try {
          yield writeSomeDataToDisk(data);
          this.state = "Data successfully written to disk";
        } catch (ex) {
          this.state = "Writing data to disk failed, proceeding with shutdown: " + ex;
        }

        yield FooService.oneLastCall();
        this.state = "Ready";
      }.bind(this)
    };


Example 4: A service with both internal and external dependencies
-----------------------------------------------------------------

 .. code-block:: javascript

    // Module FooService2

    Components.utils.import("resource://gre/modules/AsyncShutdown.jsm", this);
    Components.utils.import("resource://gre/modules/Task.jsm", this);
    Components.utils.import("resource://gre/modules/Promise.jsm", this);

    this.exports = ["FooService2"];

    let shutdown = new AsyncShutdown.Barrier("FooService2: Waiting for clients before shutting down");

    // Export the `client` capability, to let clients register shutdown blockers
    FooService2.shutdown = shutdown.client;

    // A second barrier, used to avoid shutting down while any connections are open.
    let connections = new AsyncShutdown.Barrier("FooService2: Waiting for all FooConnections to be closed before shutting down");

    let isClosed = false;

    FooService2.openFooConnection = function(name) {
      if (isClosed) {
        throw new Error("FooService2 is closed");
      }

      let deferred = Promise.defer();
      connections.client.addBlocker("FooService2: Waiting for connection " + name + " to close",  deferred.promise);

      // ...


      return {
        // ...
        // Some FooConnection object. Presumably, it will have additional methods.
        // ...
        close: function() {
          // ...
          // Perform any operation necessary for closing
          // ...

          // Don't hoard blockers.
          connections.client.removeBlocker(deferred.promise);

          // The barrier MUST be lifted, even if removeBlocker has been called.
          deferred.resolve();
        }
      };
    };


    // This Task should be triggered at some point during shutdown, generally
    // as a client to another Barrier. Triggering this Task is not covered
    // in this snippet.
    let onshutdown = Task.async(function*() {
      // Wait for all registered clients to have lifted the barrier.
      // These clients may open instances of FooConnection if they need to.
      yield shutdown.wait();

      // Now stop accepting any other connection request.
      isClosed = true;

      // Wait for all instances of FooConnection to be closed.
      yield connections.wait();

      // Now finish shutting down FooService2
      // ...
    });

.. _AsyncShutdown_phases:

Phases: Expressing dependencies towards phases of shutdown
==========================================================

The shutdown of a process takes place by phase, such as:

- ``profileBeforeChange`` (once this phase is complete, there is no guarantee that the process has access to a profile directory);
- ``webWorkersShutdown`` (once this phase is complete, JavaScript does not have access to workers anymore);
- ...

Much as services, phases have clients. For instance, all users of web workers MUST have finished using their web workers before the end of phase ``webWorkersShutdown``.

Module ``AsyncShutdown`` provides pre-defined barriers for a set of
well-known phases. Each of the barriers provided blocks the corresponding shutdown
phase until all clients have lifted their blockers.

List of phases
--------------

``AsyncShutdown.profileChangeTeardown``

  The client capability for clients wishing to block asynchronously
  during observer notification "profile-change-teardown".


``AsyncShutdown.profileBeforeChange``

  The client capability for clients wishing to block asynchronously
  during observer notification "profile-change-teardown". Once the
  barrier is resolved, clients other than Telemetry MUST NOT access
  files in the profile directory and clients MUST NOT use Telemetry
  anymore.

``AsyncShutdown.sendTelemetry``

  The client capability for clients wishing to block asynchronously
  during observer notification "profile-before-change-telemetry".
  Once the barrier is resolved, Telemetry must stop its operations.

``AsyncShutdown.webWorkersShutdown``

  The client capability for clients wishing to block asynchronously
  during observer notification "web-workers-shutdown". Once the phase
  is complete, clients MUST NOT use web workers.