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

IllegalStateException database / CursorWindow #610

Closed
HugoGresse opened this issue Jun 14, 2017 · 10 comments
Closed

IllegalStateException database / CursorWindow #610

HugoGresse opened this issue Jun 14, 2017 · 10 comments
Labels
Milestone

Comments

@HugoGresse
Copy link

I've encounterd this issue on a Nexus 5 runing 6.0.1 :

Caused by java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

FileDownloader v1.5.5

See full crash here.

It will really help if you can look into it.

@YuriHeupa
Copy link

I'm also facing the same issue. There's already 483 crash occurrences among different devices and operating system versions. See here.

@Jacksgong
Copy link
Collaborator

Jacksgong commented Jun 15, 2017

It's very weird, the column must exist but the cursor can't found it.

@HugoGresse In your exception stack:

com.liulishuo.filedownloader.services.DefaultDatabaseImpl$MaintainerIterator.next (DefaultDatabaseImpl.java:322)

It point to can not found pathAsDirectory column through cursor#getColumnIndex, but this column is added since database version 2:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion < 2) {
        String addAsDirectoryColumn = "ALTER TABLE " + DefaultDatabaseImpl.TABLE_NAME +
                " ADD COLUMN " + FileDownloadModel.PATH_AS_DIRECTORY +
                " TINYINT(1) DEFAULT 0";
        db.execSQL(addAsDirectoryColumn);

        String addFilenameColumn = "ALTER TABLE " + DefaultDatabaseImpl.TABLE_NAME +
                " ADD COLUMN " + FileDownloadModel.FILENAME +
                " VARCHAR";
        db.execSQL(addFilenameColumn);
    }
    ...
}

@YuriHeupa the stack you provided I cannot match any code on line 1329:

com.liulishuo.filedownloader.services.DefaultDatabaseImpl$MaintainerIterator.next (DefaultDatabaseImpl.java:1329)

so, could you please provide some other stacks, help me find the regular on this problem?


And could you two guys fix such problem with me, it would be nice if you can help me with this problem, and PR.

And following are related methods

  1. Create and Upgrade database: DefaultDatabaseOpenHelper#onUpgradeDefaultDatabaseOpenHelper#onCreate
  2. Problem site: DefaultDatabaseImpl.MaintainerIterator#next, two exception stack points to we get column index is -1(which means can't find) on cursor#getColumnIndex.
  3. CustomComponentHolder#maintainDatabase which using DefaultDatabaseImpl.MaintainerIterator to restore data from the database

P.S. the database is refactored since v1.5.4

@sinhpn92
Copy link

I have problem too. My log:

Fatal Exception: java.lang.RuntimeException: Unable to create service com.liulishuo.filedownloader.services.FileDownloadService$SeparateProcessService: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:3883)
       at android.app.ActivityThread.access$2100(ActivityThread.java:229)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7325)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
       at android.database.CursorWindow.nativeGetLong(CursorWindow.java)
       at android.database.CursorWindow.getLong(CursorWindow.java:524)
       at android.database.CursorWindow.getInt(CursorWindow.java:591)
       at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
       at com.liulishuo.filedownloader.services.DefaultDatabaseImpl$MaintainerIterator.next(DefaultDatabaseImpl.java:329)
       at com.liulishuo.filedownloader.services.DefaultDatabaseImpl$MaintainerIterator.next(DefaultDatabaseImpl.java:302)
       at com.liulishuo.filedownloader.download.CustomComponentHolder.maintainDatabase(CustomComponentHolder.java:163)
       at com.liulishuo.filedownloader.download.CustomComponentHolder.getDatabaseInstance(CustomComponentHolder.java:90)
       at com.liulishuo.filedownloader.services.FileDownloadManager.(FileDownloadManager.java)
       at com.liulishuo.filedownloader.services.FileDownloadService.onCreate(FileDownloadService.java:52)
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:3873)
       at android.app.ActivityThread.access$2100(ActivityThread.java:229)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7325)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

@Jacksgong Jacksgong added bug and removed need-verify labels Jun 17, 2017
@Jacksgong Jacksgong added this to the 1.5.6 milestone Jun 17, 2017
@Jacksgong
Copy link
Collaborator

Jacksgong commented Jun 17, 2017

Finally I find the Reason of the crash:

Add column executions are invoked for SQLite isn't effective immediately on DefaultDatabaseOpenHelper #onUpgrade method, because of we enable the WAL(Write-Ahead-Logging) for SQLite since v1.5.2.

So after database upgrade, we expect everything is changed on the database but NOT............... so some column can't find...

When does your user can face this crash?

The following condition must be met at the same time:

  1. Your user upgrades the filedownloader from v1.4.3 or older version jump to the v1.5.2 or newer version directly(because WAL is enabled since v1.5.2 )
  2. Your user used filedownloader to download something( So the database has been created )
  3. There are something downloaded tasks are paused on breakpoint and stored on the database( So we would invoke maintainDatabase method )
  4. The system version of the android phone is equal to or greater than JELLY_BEAN ( because the SQLiteOpenHelper#setWriteAheadLoggingEnabled(boolean) is added since JELLY_BEAN)

BUT This crash only can be raised once since user upgrade filedownloader, because the next time the database upgrade has been affected.

THIS ISSUE WILL BE FIXED ON 1.5.6

On the v.1.5.2 I enable the WAL with opening the database ( through SQLiteOpenHelper#setWriteAheadLoggingEnabled(boolean) on the constructor of the implementation of SQLiteOpenHelper ).

The solution is delaying the WAL-enable-operation since the database is opened( on the SQLiteOpenHelper#onOpen(SQLiteDatabase) method, the database has been onCreate or onUpgrade).

@HugoGresse
Copy link
Author

Wooo that was a really complicated issue :/ Thanks for working on it!

@sinhpn92
Copy link

Thanks so much

@YuriHeupa
Copy link

Sorry for the late response.. and thanks for looking into this, really appreciated!!

@HugoGresse
Copy link
Author

did you guyes are able to use the 1.5.6 @sinhpn92 @YuriHeupa Androdi Studio is unable to import the classes :/

@YuriHeupa
Copy link

@HugoGresse Yeh, I did. Try cleaning then rebuilding the project.

@HugoGresse
Copy link
Author

Yep I did, somehting got wrong with AS see #620

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

No branches or pull requests

4 participants