News for hackers. Stuff that matters.
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @03:38PM (#12914378)
I had to go through a lot of comments in that thread to find a working workaround.

On OS X, Open /Applications/Spotify.app/Contents/MacOS/Spotify in a hex editor.

Search for "VACUUM;" Replace with "xxxxxx;"

On Windows, apparently the key "VACUUM;" string is in libcef.dll but I don't have a Windows system to see if editing it the same way provides a workaround like on OS X.

I used hexcurse from homebrew. After opening the file hit tab once to switch to the ASCII side, Control-F to search for VACUUM;, then type "x" six times to overwrite each character. Quit and save.

EDIT: fixes

Re:Spotify excessively writing to drive by tomovo on Wednesday November 09, 2016 @03:55PM (#12914540)
Hex Fiend is another hex editor for the Mac. Native and fast. http://ridiculousfish.com/hexfiend/
Re:Spotify excessively writing to drive by wamatt on Thursday November 10, 2016 @03:28AM (#12918675)
Or do it with a one liner

perl -pi -e 's/VACUUM/xxxxxx/g' /Applications/Spotify.app/Contents/MacOS/Spotify

Re:Spotify excessively writing to drive by arcticbull on Thursday November 10, 2016 @01:31AM (#12918190)
Re:Spotify excessively writing to drive by barbs on Wednesday November 09, 2016 @05:42PM (#12915517)
If you're looking for vi, but for binary, bvi does the job.

http://bvi.sourceforge.net/

Re:Spotify excessively writing to drive by kovrik on Wednesday November 09, 2016 @08:39PM (#12916912)
Or use hexl-mode in Emacs
Re:Spotify excessively writing to drive by JadeNB on Thursday November 10, 2016 @11:47AM (#12921953)
I'm pretty sure vi itself, or at least vim, is fine with binary files. You can run it as `vim -b` or `:set binary`.
Re:Spotify excessively writing to drive by elsurudo on Wednesday November 09, 2016 @03:45PM (#12914455)
An explanation of what this does?

Also, I'm assuming this has to be done after every update, and since Spotify auto-updates, not so convenient...

Re:Spotify excessively writing to drive by cosmie on Wednesday November 09, 2016 @04:07PM (#12914661)
It likely just stops Spotify from VACUUM'ing the SQLite database it has.

Spotify stores a local SQLite database file (by default on a Mac it's at ~/Library/Application Support/Spotify/PersistentCache/mercury.db )

The file is only about ~100MB (on my computer). With databases, you issue a vacuum command to defragment a database (reclaim space from deleted/updated rows, re-sort the data, etc). SQLite's VACUUM behavior basically just recreates the entire database from scratch in a temp file, then replaces the live file with that[1]. It makes the file smaller and more efficient to run queries against. Spotify must be triggering a VACUUM statement too aggressively (possibly after every change), rather than on a periodic schedule or after a certain amount of fragmentation. By breaking the VACUUM statement with this solution, you prevent the database from getting recreated so frequently. The file itself will grow slowly (since you're not reclaiming free space like a VACUUM would) and possibly slow down queries against the database, but that sounds like a far better tradeoff than excessive wear on the drive from recreating the file so often.

[1]https://sqlite.org/lang_vacuum.html

Re:Spotify excessively writing to drive by ChoGGi on Thursday November 10, 2016 @12:42AM (#12918049)
Once you apply that fix you can manually vacuum with

  sqlite3 mercury.db vacuum;
from https://sqlite.org/download.html
Re:Spotify excessively writing to drive by pasbesoin on Wednesday November 09, 2016 @05:38PM (#12915478)
Years ago, manually vacuuming the several SQLite databases that Firefox used (uses) was often cited as a way to speed up Firefox, meaning particularly an instance/profile that had been heavily used for some time. Not sure about these days, though the other week something had me in Bleachbit (IIRC) and I saw that doing so is still one of that application's selectable functions.
Re:Spotify excessively writing to drive by thirdsun on Thursday November 10, 2016 @02:02AM (#12918327)
That sounds like an incredibly easy thing for Spotify to fix - how is this still an issue?

On the other hand it fits the picture: Their clients/apps on multiple platforms are very underwhelming. The abandoned rdio apps back from 2014 (or when did they close?) would offer a better experiment in almost any aspect.

Re:Spotify excessively writing to drive by cosmie on Thursday November 10, 2016 @04:00PM (#12924303)
I can't find the article right now, but I read a while ago that the Spotify client applications are modularized, such that different feature teams can push updates to their individual feature without breaking or being interdependent on other parts of the clients releasing in sync. I'm not sure what the mercury database is used for or if it's maintained by one of those "feature teams" or is part of the core lib, but that lack of central oversight (and responsibility) could be one reason why such an easy fix isn't so easy to get fixed.
Re:Spotify excessively writing to drive by verytrivial on Thursday November 10, 2016 @04:53AM (#12919069)
Agreed.

  if (time_since_last_vacuum > 3600 /* seconds */) {
     go_hogwild_on_io();
  } else {
     be_nice();
  }
Re:Spotify excessively writing to drive by 72deluxe on Thursday November 10, 2016 @05:02AM (#12919108)
Yes, this is an incredibly easy fix. Perhaps it is me but sometimes it seems that nobody wants to write decent software anymore. I have seen software from giants like Cisco and BT that is abysmal.

It is disheartening to those of us who enjoy writing decent software, particularly decent native software.

Re:Spotify excessively writing to drive by peeters on Wednesday November 09, 2016 @03:52PM (#12914511)
I'm more familiar with Postgres, but it basically instructs the table storage provider to clean up unused disk.

Think of it roughly as defragmentation. You're trading work (in the form of writes and CPU) for reclaimed space.

Unless SQLite has some semantics around it, replacing it with "xxxxxx;" will likely just cause it to run a bad command...might be more correct to replace it with something like "------;" (comment characters).

Re:Spotify excessively writing to drive by boulos on Wednesday November 09, 2016 @11:10PM (#12917647)
I was going to say "SELECT 1;" but it's two characters too many. Will all comment characters evaluate to whatever VACUUM does?
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @03:49PM (#12914478)
I didn't read all of the original thread but apparently Spotify is overusing sqlite VACUUM, causing a modest amount of data to turn into a huge write volume.

https://sqlite.org/lang_vacuum.html

Re:Spotify excessively writing to drive by milankragujevic on Wednesday November 09, 2016 @04:00PM (#12914594)
Hmmm, could it be an issue with Chromium Embedded Framework, give that it uses SQLite and it's in libcef.dll? I uninstalled Spotify from my MBA but I'm not sure whether the damage was already done...
Re:Spotify excessively writing to drive by adrinavarro on Wednesday November 09, 2016 @03:53PM (#12914525)
This fixes it, but it's likely any future update kills this temp fix. Hopefully Spotify will take care of this. I also noticed the extremely high writes less than a week ago. Wonder how long this has been going on.
Re:Spotify excessively writing to drive by Etheryte on Wednesday November 09, 2016 @03:48PM (#12914475)
Use <Control-F> to search and <Control-S> to save, it seems you mixed them up in your otherwise very helpful comment. What's the source of the problem and the fix, though?
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @03:49PM (#12914482)
Yep, mixed them up. See other comment about sqlite.
Re:Spotify excessively writing to drive by voltagex_ on Wednesday November 09, 2016 @05:55PM (#12915654)
That's a pretty cool fix. I wonder how they found that. This also highlights another issue with closed source software (and a few open source apps too): complete opaqueness. Yeah, hide the logging and status by default but at least give me a way to see what's actually going on. The Spotify Android app is worse. I wonder if it also suffers from this issue (I swapped to Apple Music after losing all of my offline tracks in Spotify again)
Re:Spotify excessively writing to drive by 72deluxe on Thursday November 10, 2016 @05:05AM (#12919117)
They probably just monitored what files the spotify client had open, or on Windows uses something like Process Monitor and monitored the program's activities. It would have seen a file open command pretty soon.

And once they saw it open the sqlite.db, all they needed to do was run sqlite3 on the command line against the DB file and see if there was history in the file.

It is possible that the Android client does the same as sqlite3 is the underlying database format for apps and is very easy to use.

Re:Spotify excessively writing to drive by jhack on Wednesday November 09, 2016 @05:00PM (#12915133)
Great fix. Only 1.7MB written in about 45 minutes.
Re:Spotify excessively writing to drive by joemi on Wednesday November 09, 2016 @06:07PM (#12915759)
Does anyone know why "xxxxxx;"? Seems like that would make an invalid call, so wouldn't it make more sense to replace it with some sort of no-op? Or is xxxxxx sql's way of doing a no-op?
Re:Spotify excessively writing to drive by CGamesPlay on Wednesday November 09, 2016 @08:27PM (#12916830)
Because that breaks the statement, Spotify doesn't care if this particular statement succeeds or fails, and because it has the same length as "VACUUM;". Since this is being modified in a binary, you can't change the length of the string, or else you'll overwrite some other part of the data.
Re:Spotify excessively writing to drive by jleahy on Wednesday November 09, 2016 @06:54PM (#12916191)
Presumably it results in an error, but if the return value isn't checked then that's fine.
Re:Spotify excessively writing to drive by pbhjpbhj on Wednesday November 09, 2016 @08:02PM (#12916682)
DO 0

or

SET @noop=0

or

NULL; -- works in pg at least

seem like good possibilities.

Re:Spotify excessively writing to drive by heheocoenev on Wednesday November 09, 2016 @06:04PM (#12915744)
How does this not break the signature on the binary and stop execution?
Re:Spotify excessively writing to drive by toyg on Wednesday November 09, 2016 @07:32PM (#12916464)
is Spotify signed at all? Anyway, worked for me (used '------;' with the python script above).
Re:Spotify excessively writing to drive by brazzledazzle on Wednesday November 09, 2016 @10:24PM (#12917449)
It's signed on my laptop.
Re:Spotify excessively writing to drive by brazzledazzle on Wednesday November 09, 2016 @10:25PM (#12917457)
Mine is signed. Maybe the HN crowd tends to have less restrictive GateKeeper settings since running unsigned stuff is kind of a pain in the ass.
Re:Spotify excessively writing to drive by ihuman on Wednesday November 09, 2016 @03:44PM (#12914446)
6 or 7 x's? In your example, you used 6.
Re:Spotify excessively writing to drive by cjg_ on Wednesday November 09, 2016 @03:48PM (#12914476)
6, it wont start otherwise. I left the ; in.
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @03:50PM (#12914490)
Hmm, I used 7 and my patched client is running fine. Maybe it's enough to garble VACUUM so it doesn't do anything valid.
Re:Spotify excessively writing to drive by cjg_ on Wednesday November 09, 2016 @03:58PM (#12914577)
If you replaced the ; also, you also didn't change the total length of the strings, I think that's what the problem is.
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @03:47PM (#12914468)
7 x's, one to overwrite all of VACUUM; including the terminating semicolon.

I didn't count the x's in the quoted text that I copied from the thread.

Re:Spotify excessively writing to drive by 0x0 on Wednesday November 09, 2016 @10:58PM (#12917603)
If you overwrite the ; is there not a risk you might cause the next legitimate sqlite operation to fail?
Re:Spotify excessively writing to drive by SturgeonsLaw on Thursday November 10, 2016 @05:41AM (#12919286)
They might get lucky and overwrite the last SQL statement in the method. Seems likely with a statement like VACUUM.
Re:Spotify excessively writing to drive by micaksica on Wednesday November 09, 2016 @10:31PM (#12917488)
This patch also works fine on Spotify for Linux if you patch with ghex.
Re:Spotify excessively writing to drive by agopaul on Wednesday November 09, 2016 @05:35PM (#12915452)
iHex is an alternative (native) hex editor and it's in the Mac App Store https://itunes.apple.com/us/app/ihex-hex-editor/id909566003?...
Re:Spotify excessively writing to drive by mikeash on Wednesday November 09, 2016 @03:54PM (#12914534)
Spotify updates itself fairly regularly, so you may need to reapply this fix from time to time.
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @04:05PM (#12914645)
Brute-force solution: save this as spotify-fixer.py, run it as cron job:

  #!/usr/bin/python

  target = "/Applications/Spotify.app/Contents/MacOS/Spotify"
  with open(target) as infile:
      bytes = infile.read()
  with open("./Spotify", "wb") as backup:
      backup.write(bytes)
  fixed = bytes.replace("VACUUM;", "xxxxxx;")
  with open (target, "wb") as outfile:
      outfile.write(fixed)
Re:Spotify excessively writing to drive by unhammer on Thursday November 10, 2016 @06:51AM (#12919629)
On Ubuntu/Debian, it might make sense to run it after each apt-get:

    $ cat /usr/local/bin/spotify-turn-off-vacuum 
    #!/usr/bin/env python2
    target = "/usr/bin/spotify"
    with open(target) as infile:
        bytes = infile.read()
    with open(target+".bak", "wb") as backup:
        backup.write(bytes)
    fixed = bytes.replace("VACUUM;", "xxxxxx;")
    with open (target, "wb") as outfile:
        outfile.write(fixed)
    
    $ cat /etc/apt/apt.conf.d/99spotify-turn-off-vacuum
    DPkg::Post-Invoke {"/usr/local/bin/spotify-turn-off-vacuum";};
Re:Spotify excessively writing to drive by Someone on Thursday November 10, 2016 @03:23AM (#12918655)
If you do that, go the extra mile and schedule a (weekly or so) job to vacuum that database.

Also, since you are on a Mac, I would use launchd jobs and make it less brute-force by scheduling the job changing the file on writes to the Applications/Spotify.app/Contents/MacOS/ directory.

Re:Spotify excessively writing to drive by tdkl on Thursday November 10, 2016 @05:22AM (#12919198)
It's beyond me, why doesn't Spotify do exactly the same thing instead of overusing the VACUUM command.
Re:Spotify excessively writing to drive by toomuchtodo on Wednesday November 09, 2016 @04:31PM (#12914866)
[retracted]

Thanks logicallee! I definitely need to be more judicious about time management, so I appreciated your comment!

Re:Spotify excessively writing to drive by logicallee on Wednesday November 09, 2016 @04:35PM (#12914902)
dude. stop. the reason your username is "toomuchtodo" is shit like this. Just do it RIGHT NOW, or don't do it. It's not that important. Delete that stupid bookmark.

Just because something could be fun to do if you didn't have anythign else to do and were bored out of your mind, doesn't mean it's worth doing, ever.

Re:Spotify excessively writing to drive by mynameisvlad on Wednesday November 09, 2016 @07:59PM (#12916659)
Is the vitrol really necessary? How does what a complete stranger decides to do in their spare time affect you in any way shape or form?
Re:Spotify excessively writing to drive by smhost on Wednesday November 09, 2016 @08:55PM (#12917005)
I think it's more tough love than it is vitriol.
Re:Spotify excessively writing to drive by toomuchtodo on Wednesday November 09, 2016 @10:41PM (#12917528)
> I think it's more tough love

That's how I took it.

Re:Spotify excessively writing to drive by logicallee on Thursday November 10, 2016 @03:30AM (#12918686)
yeah
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @04:36PM (#12914911)
If you're on a Mac you have python already. Just save it somewhere and curl pipe it to python.
Re:Spotify excessively writing to drive by 45h34jh53k4j on Wednesday November 09, 2016 @06:22PM (#12915901)
No No No, please do not encourage people to do this. Curl pipe is a horrible anti security pattern and you are encouraging people to be irresponsible with patching binaries from the internet.
Re:Spotify excessively writing to drive by dguaraglia on Wednesday November 09, 2016 @07:28PM (#12916427)
The weird thing is talking about "curl pipe"ing into Python when you have a local copy? Wha?
Re:Spotify excessively writing to drive by philipkglass on Wednesday November 09, 2016 @07:44PM (#12916545)
The now-retracted comment talked about rewriting the Python into a shell script that you could toss on a web server and curl-pipe to run. I was pointing out that you can curl-pipe the original Python without rewriting it as shell.
Re:Spotify excessively writing to drive by ulucs on Thursday November 10, 2016 @03:28AM (#12918676)
for Linux, it is at /usr/share/spotify/spotify.