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

TypeError: Cannot read property 'authorized' of undefined #9

Closed
dgthistle opened this issue Aug 5, 2011 · 11 comments
Closed

TypeError: Cannot read property 'authorized' of undefined #9

dgthistle opened this issue Aug 5, 2011 · 11 comments
Assignees

Comments

@dgthistle
Copy link

I'm seeing this error periodically:

/usr/local/lib/node_modules/apn/lib/apn.js:50
self.socket.pair.on('secure', function () { if(!self.socket.authorized) { th
^
TypeError: Cannot read property 'authorized' of undefined
at SecurePair. (/usr/local/lib/node_modules/apn/lib/apn.js:50:62)
at SecurePair.emit (events.js:81:20)
at SecurePair._maybeInitFinished (tls.js:581:10)
at CleartextStream._pull (tls.js:350:15)
at SecurePair._cycle (tls.js:548:20)
at EncryptedStream.write (tls.js:94:13)
at Socket.ondata (stream.js:36:26)
at Socket.emit (events.js:64:17)
at Socket._onReadable (net.js:673:14)
at IOWatcher.onReadable as callback

I believe this occurs when the APN gateway is unreachable and the fix is to simply check the socket.

var startSocket = function () {
    self.socket = tls.connect(options['port'], options['gateway'], socketOptions);
    if (self.socket !== undefined) {
    self.socket.pair.on('secure', function () { if(!self.socket.authorized) { throw self.socket.authorizationError } while(offlineCache.length) { self.socket.write(offlineCache.shift()); } });
    self.socket.on('data', function(data) { handleTransmissionError(data); });
    self.socket.on('error', function(data) {self.socket.removeAllListeners(); self.socket = undefined; });
    self.socket.once('end', function () {  });
    self.socket.once('close', function () { self.socket.removeAllListeners(); self.socket = undefined; });
    }
}

The offline cache will get populated and the note sent later.

@argon
Copy link
Collaborator

argon commented Aug 8, 2011

Thanks for highlighting this, I was always a bit suspicious of my use of self.socket.pair.on(), its looks like maybe I should use the callback on tls.connect which I somehow missed when I ported to 0.4, I shall investigate and get it fixed ASAP.

@dgthistle
Copy link
Author

Thanks Argon! Sounds like the right approach. I'd be more than happy to help with the fix or testing the fix. Just let me know.

@ghost ghost assigned argon Aug 11, 2011
@argon
Copy link
Collaborator

argon commented Aug 11, 2011

Well, I should be using the callback on tls.connect() but that is not the problem. The error is within the callback and for some reason self.socket is undefined so the property authorized cannot be read. Maybe the object is getting destroyed when it cannot connect but further investigation is required.

@argon
Copy link
Collaborator

argon commented Aug 15, 2011

I have been unable to replicate the problem and I can't quite figure out what could be causing the problem.

Are you able to provide any example to reliably replicate the problem?

@dgthistle
Copy link
Author

I have finally found a fairly reliable way to reproduce by forcing multiple InvalidToken error conditions when sending a push notification (I think my original assumption that it was related to an apn service connection error is wrong). In my case, the token is actually a developer profile token being sent to the production gateway, but I presume it'll work for any invalid token.

I believe the error occurs in other "valid" cases but haven't confirmed yet. Will do when I find another case.

@dgthistle
Copy link
Author

In my development environment, I get a different exception but along the same lines:

assert.js:81
throw new assert.AssertionError({
^
AssertionError: true == false
at CleartextStream.readyState (net.js:285:7)
at [object Object].sendNotification (/Users/dgthistle/local/node/lib/node_modules/apn/lib/apn.js:118:47)
at /Users/dgthistle/local/node/lib/node_modules/apn/lib/apn.js:160:10
at CleartextStream. (/Users/dgthistle/local/node/lib/node_modules/apn/lib/apn.js:51:43)
at CleartextStream.emit (events.js:64:17)
at CleartextStream._push (tls.js:313:12)
at SecurePair.cycle (tls.js:594:20)
at EncryptedStream.write (tls.js:124:13)
at Socket.ondata (stream.js:36:26)
at Socket.emit (events.js:64:17)

Haven't quite grasped why the path is different. I simply force two InvalidToken (8) errors in a row to generate this.

@dgthistle
Copy link
Author

OK. Scratch that last comment... I updated to the latest version of node (v4.11) and I'm back to the same exception:

/Users/dgthistle/local/node/lib/node_modules/apn/lib/apn.js:50
self.socket.pair.on('secure', function () { if(!self.socket.authorized) { th
^
TypeError: Cannot read property 'authorized' of undefined
at SecurePair. (/Users/dgthistle/local/node/lib/node_modules/apn/lib/apn.js:50:62)
at SecurePair.emit (events.js:81:20)
at SecurePair.maybeInitFinished (tls.js:601:10)
at CleartextStream._push (tls.js:269:17)
at SecurePair.cycle (tls.js:577:20)
at EncryptedStream.write (tls.js:96:13)
at Socket.ondata (stream.js:36:26)
at Socket.emit (events.js:64:17)
at Socket._onReadable (net.js:678:14)
at IOWatcher.onReadable as callback

The exception in the last comment was related to nodejs/node-v0.x-archive#1069 and was fixed with an upgrade.

@dgthistle
Copy link
Author

Assuming this only happens when the access token is invalid, I'll submit a pull request that just ignores the situation. If I see it occurring in other circumstances I'll let you know.

@argon
Copy link
Collaborator

argon commented Aug 29, 2011

The fact that it happens when the access code is invalid is interesting, I am currently running through the code to find the possible problem.

@argon
Copy link
Collaborator

argon commented Aug 29, 2011

I have succeeded in replicating the problem. WIth device token being incorrect.

var device = new APNService.device(token);
var alert = "Message received from node-apn!";
var note = new APNService.notification();
note.alert = alert;
note.badge = 1;
note.device = device;

var note2 = new APNService.notification();
note2.alert = alert;
note2.device = device;
try {
    service.sendNotification(note);
    service.sendNotification(note2);
}
catch (e) {
    console.log(e);
}

@argon
Copy link
Collaborator

argon commented Aug 29, 2011

I have pushed a patch to the 'develop' branch for this issue. Would you mind checking it out to ensure it fixes the problem at your end?

@argon argon closed this as completed in 052eabf Aug 31, 2011
hharanm pushed a commit to hharanm/node-apn that referenced this issue Apr 16, 2023
Fix: http2session.shutdown() is not a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants