Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

[ios] intermittent CORS error #119

Open
cboden opened this issue Mar 24, 2017 · 12 comments
Open

[ios] intermittent CORS error #119

cboden opened this issue Mar 24, 2017 · 12 comments

Comments

@cboden
Copy link

cboden commented Mar 24, 2017

Quite often things work but every so often the HTTP request to CBL either sort of or fully fails with:

Error: Request has been terminated Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc. at Request.crossDomainError (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:100176:9) at XMLHttpRequest.xhr.onreadystatechange (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:100244:13) at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7805:15) at XMLHttpRequest.setReadyState (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:34051:6) at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:33895:6) at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:33999:52 at RCTDeviceEventEmitter.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:6900:23) at MessageQueue.__callFunction (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:4296:34) at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:4168:7 at guard (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:4106:1)

Doing the exact same test-case every time sometimes the document will save, sometimes not. And sometimes the document will be saved but this error still happens.

What can I do to fix this or what additional info can I provide to further troubleshoot this. I'm also attaching the screenshot of the console.error:

screen shot 2017-03-24 at 12 05 42 pm

@tienlm1509
Copy link

I have same issue !

@jamesnocentini
Copy link
Contributor

sorry to hear you've been running into those issues. I've seen that a few times too but couldn't tell why. @cboden are you still seeing those errors?

Since the app isn't running in a browser, I think we can rule out CORS as the root cause. the network is offline is something to look into, maybe the request is sent before the listener has had a change to start.

@jamesnocentini jamesnocentini self-assigned this Apr 13, 2017
@jamesnocentini jamesnocentini changed the title Intermittent failures [Android] intermittent errors in requests to the listener Apr 13, 2017
@jamesnocentini jamesnocentini changed the title [Android] intermittent errors in requests to the listener Intermittent failures Apr 13, 2017
@cboden
Copy link
Author

cboden commented Apr 13, 2017

Yup, we haven't released our app yet, we see this in development all day every day. I'm currently testing and seeing this on iOS.

I will see this error mid-way through running the app; I'll do some queries, some inserts, at some point try to do another insert and get this error.

@jamesnocentini
Copy link
Contributor

Is the issue happening on Android as well or iOS only?

I recently found out that the iOS Listener doesn't support OPTIONS requests which is something the browser would do when it sends a request to a different origin than the web server. But again, in the context of react native I don't know if it's relevant.

@cboden
Copy link
Author

cboden commented Apr 13, 2017

Android is currently untested; we haven't ported it over yet.

@jamesnocentini jamesnocentini changed the title Intermittent failures [ios] Intermittent failures Apr 13, 2017
@jamesnocentini
Copy link
Contributor

ok so it's likely to happen on iOS only at this stage and the error message is exactly the same as the Chrome logs from couchbase/couchbase-lite-ios#1709. The scenario is different (web app versus react native app) but it'll be good to try it again with the fix for 1709 to see if it makes any difference.

@jamesnocentini jamesnocentini changed the title [ios] Intermittent failures [ios] intermittent CORS errors Apr 13, 2017
@jamesnocentini jamesnocentini changed the title [ios] intermittent CORS errors [ios] intermittent CORS error Apr 13, 2017
@djpongh djpongh added the bug label Apr 14, 2017
@juanpgaviria
Copy link

Hi, im having the same issue, but it like 50% of UPDATE request. Does anyone has a solution or workaround?

@climam
Copy link

climam commented Aug 18, 2017

Hello,

I am also facing this issue very frequently. @jamiltz, if I understand well your comment, this issue is related to couchbase-lite-ios library. Right ? Does it mean that it is not possible to make PUT http request to standard ios couchbase ? I wonder why this issue is still pending. Is it a feature only used by react-native plugin ?

Best regards,
Cyril.

@climam
Copy link

climam commented Aug 23, 2017

I have added following function in CBL_Router+Handlers.m as suggested in couchbase/couchbase-lite-ios#1709 but it does not work:

  • (CBLStatus) do_OPTIONS {
    return kCBLStatusOK;
    }

Still have the same issue:

{ url: 'http://u72321505:p26009043@localhost:5984/booqee/Transaction%3A%3A1503528799142-e42983a1-8577-47fd-b2d2-13cf32ff198a',
method: 'PUT',
headers: { error: 'no response from server' },
errObj:
{ [Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.]
line: 89496,
column: 22,
sourceURL: 'http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false',
crossDomain: true,
status: undefined,
method: 'PUT',
url: 'http://u72321505:p26009043@localhost:5984/booqee/Transaction%3A%3A1503528799142-e42983a1-8577-47fd-b2d2-13cf32ff198a' },
status: 0,
statusText: undefined,
data: undefined }

@Matherunner
Copy link

I can reproduce this bug reasonably consistently in the following way: call manager.query.get_db_design_ddoc_view_view twice or more times in quick succession (with the next call made immediately after awaiting the previous call), then following a few document update, delete, or add operations this bug will eventually be triggered.

If only one call to that function is made, then this error will not appear at all. On the other hand, if the calls to get_db_design_ddoc_view_view overlap temporally (i.e. calling it the second time without waiting for the first to complete), then the bug seems to trigger with greater consistency.

Beside the function above, this bug may also be reproduced by calling other functions in quick succession, though I've not tested them.

Until this bug is resolved, this library is regrettably unusable.

@cboden
Copy link
Author

cboden commented Sep 11, 2017

This is far from idea but it's a temporary solution we're using. We're using ReactiveX/rxjs to handle all our I/O including CBL. We've created a standard function to replace dbManager.document.put that returns an Observable instead of its standard promise and we've chained it to look like this:

export const putDoc = (db: string, docId: string, doc: object): Observable<object> => managerReady$
  .flatMap(dbManager => dbManager.document.put({db, doc: docId, body: doc}))
  .retryWhen(attempts$ => attempts$
    .scan((errorCount, err) => {
      if (errorCount >= 3) {
        throw err;
      }

      return errorCount + 1;
    }, 0)
    .delayWhen(i => Observable.timer(i * 50))
  )
  .map((result: CouchbaseWriteResult) => {
    if (result.status >= 300 || result.status < 200) {
      throw result;
    }

    return doc;
  });

When the write attempt fails it will retry 3 times with a slight exponential backoff before bubbling the error up. Hope that can help someone who's stuck like we have been.

@JasonMore
Copy link

I've been seeing this too. Thanks for the advice everyone

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants