summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/socket_disconnect_kills.js
blob: cf79caf45550dbac59697e8c1d952e470adea00b (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
// This test verifies that particular code paths exit early (I.e. are killed) or not by:
//
// 1. Set a fail point that will hang the code path
// 2. Open a new client with sockettimeoutms set (to force the disconnect) and a special appname
//    (to allow easy checking for the specific connection)
// 3. Run the tested command on the special connection and wait for it to timeout
// 4. Use an existing client to check current op for that special appname.  Return true if it's
//    still there at the end of a timeout
// 5. Disable the fail point
//
// @tags: [requires_sharding]

(function() {
"use strict";

const testName = "socket_disconnect_kills";

// Used to generate unique appnames
let id = 0;

// client - A client connection for curop (and that holds the hostname)
// pre - A callback to run with the timing out socket
// post - A callback to run after everything else has resolved (cleanup)
//
// Returns false if the op was gone from current op
function check(client, pre, post) {
    const interval = 200;
    const timeout = 10000;
    const socketTimeout = 5000;

    const host = client.host;

    // Make a socket which will timeout
    id++;
    let conn =
        new Mongo(`mongodb://${host}/?socketTimeoutMS=${socketTimeout}&appName=${testName}${id}`);

    // Make sure it works at all
    assert.commandWorked(conn.adminCommand({ping: 1}));

    try {
        // Make sure that whatever operation we ran had a network error
        assert.throws(function() {
            try {
                pre(conn);
            } catch (e) {
                if (isNetworkError(e)) {
                    throw e;
                }
            }
        }, [], "error doing query: failed: network error while attempting");

        // Spin until the op leaves currentop, or timeout passes
        const start = new Date();

        while (1) {
            if (!client.getDB("admin")
                     .aggregate([
                         {$currentOp: {localOps: true}},
                         {$match: {appName: testName + id}},
                     ])
                     .itcount()) {
                return false;
            }

            if (((new Date()).getTime() - start.getTime()) > timeout) {
                return true;
            }

            sleep(interval);
        }
    } finally {
        post();
    }
}

function runWithCuropFailPointEnabled(client, failPointName) {
    return function(entry) {
        entry[0](client,
                 function(client) {
                     assert.commandWorked(client.adminCommand({
                         configureFailPoint: failPointName,
                         mode: "alwaysOn",
                         data: {shouldCheckForInterrupt: true},
                     }));

                     entry[1](client);
                 },
                 function() {
                     assert.commandWorked(
                         client.adminCommand({configureFailPoint: failPointName, mode: "off"}));
                 });
    };
}

function runWithCmdFailPointEnabled(client) {
    return function(entry) {
        const failPointName = "waitInCommandMarkKillOnClientDisconnect";

        entry[0](client,
                 function(client) {
                     assert.commandWorked(client.adminCommand({
                         configureFailPoint: failPointName,
                         mode: "alwaysOn",
                         data: {appName: testName + id},
                     }));

                     entry[1](client);
                 },
                 function() {
                     assert.commandWorked(
                         client.adminCommand({configureFailPoint: failPointName, mode: "off"}));
                 });
    };
}

function checkClosedEarly(client, pre, post) {
    assert(!check(client, pre, post), "operation killed on socket disconnect");
}

function checkNotClosedEarly(client, pre, post) {
    assert(check(client, pre, post), "operation not killed on socket disconnect");
}

function runCommand(cmd) {
    return function(client) {
        assert.commandWorked(client.getDB(testName).runCommand(cmd));
    };
}

function runTests(client) {
    let admin = client.getDB("admin");

    // set timeout for js function execution to 100 ms to speed up tests that run inf loop.
    assert.commandWorked(client.getDB(testName).adminCommand(
        {setParameter: 1, internalQueryJavaScriptFnTimeoutMillis: 100}));
    assert.commandWorked(client.getDB(testName).test.insert({x: 1}));
    assert.commandWorked(client.getDB(testName).test.insert({x: 2}));
    assert.commandWorked(client.getDB(testName).test.insert({x: 3}));

    [[checkClosedEarly, runCommand({find: "test", filter: {}})],
     [
         checkClosedEarly,
         runCommand({
             find: "test",
             filter: {
                 $where: function() {
                     sleep(100000);
                 }
             }
         })
     ],
     [
         checkClosedEarly,
         runCommand({
             find: "test",
             filter: {
                 $where: function() {
                     while (true) {
                     }
                 }
             }
         })
     ],
    ].forEach(runWithCuropFailPointEnabled(client, "waitInFindBeforeMakingBatch"));

    // After SERVER-39475, re-enable these tests and add negative testing for $out cursors.
    const serverSupportsEarlyDisconnectOnGetMore = false;
    if (serverSupportsEarlyDisconnectOnGetMore) {
        [[
            checkClosedEarly,
            function(client) {
                let result = assert.commandWorked(
                    client.getDB(testName).runCommand({find: "test", filter: {}, batchSize: 0}));
                assert.commandWorked(client.getDB(testName).runCommand(
                    {getMore: result.cursor.id, collection: "test"}));
            }
        ]].forEach(runWithCuropFailPointEnabled(client,
                                                "waitAfterPinningCursorBeforeGetMoreBatch"));
    }

    [[checkClosedEarly, runCommand({aggregate: "test", pipeline: [], cursor: {}})],
     [checkNotClosedEarly, runCommand({aggregate: "test", pipeline: [{$out: "out"}], cursor: {}})],
    ].forEach(runWithCmdFailPointEnabled(client));

    [[checkClosedEarly, runCommand({count: "test"})],
     [checkClosedEarly, runCommand({distinct: "test", key: "x"})],
     [checkClosedEarly, runCommand({hello: 1})],
     [checkClosedEarly, runCommand({listCollections: 1})],
     [checkClosedEarly, runCommand({listIndexes: "test"})],
    ].forEach(runWithCmdFailPointEnabled(client));
}

{
    let proc = MongoRunner.runMongod();
    assert.neq(proc, null);
    runTests(proc);
    MongoRunner.stopMongod(proc);
}

{
    let st = ShardingTest({mongo: 1, config: 1, shards: 1});
    runTests(st.s0);
    st.stop();
}
})();